I have considered an object 'genObj'. This object can be of the class phones, laptops, shoes, or books. I wish to take user input varying from 1-4, and accordingly assign genObj to a praticular class.
I wish to do this because I have to add these objects to data files. I am maintaining 4 separate data files for the different classes, and a value to fileName is also assigned accordingly.
switch(mode)
{
case 1:
phones genObj;
strcpy(fileName,"phonesDatabase.dat");
break;
case 2:
computers genObj;
strcpy(fileName,"computersDatabase.dat");
break;
case 3:
shoes genObj;
strcpy(fileName,"shoesDatabase.dat)";
break;
case 4:
books genObj;
strcpy(fileName,"booksDatabase.dat");
break;
}
Doing this, hovewer, gives the following error:
"Multiple declaration for genObj in function add(int)"
The values are assigned to fileName without problem as I have declared
char fileName[20];
globally, and it gets initialized later. However, since the object genObj can be of different classes, it cannot be initialized like this.
If I am not able to assign genObj different classes according to user input, I will have to separate the code into 4 parts, each for one class. and that would be too cumbersome, and inelegant.
Thanks.
strcpyfrom your vocabulary.