Hi all
How can i use operator << for each type using polymorphism
The code bellow.
bye
lior
ofstream vanFile("c:/van.txt");
ofstream sportsFile("c:/sports.txt");
ofstream regularFile("c:/regular.txt");
Car* carP;
for (int i=0;i<12;i++)
{
carP=arr[i];
if (typeid(*carP) == typeid(Van))
{
vanFile<<*arr[i]<<endl;
}
else
if (typeid(*carP) == typeid(Sports))
sportsFile<<*arr[i]<<endl;
else
if (typeid(*carP) == typeid(Regular))
regularFile<<*arr[i]<<endl;
}
vanFile.close();
sportsFile.close();
regularFile.close();
Thinker
03-23-2002, 07:37 AM
You will have to create your own overloaded operator in your
Car class. If Van, Sports, and Regular are classes that inherit from
Car, then your operator function should be overloaded in each.
hi
the operator << is overload in each class.
but how can i use Van operatore if the arr[] is car *arr[];
i alway call the car << operator insted of the van or regular operatore.
bye
lior
Thinker
03-23-2002, 02:30 PM
Ok, I see your problem now. I am not a C++ programmer, but if
it worked the same as in VB, it would be something like...
ofstream vanFile("c:/van.txt");
ofstream sportsFile("c:/sports.txt");
ofstream regularFile("c:/regular.txt");
Car* carP;
Van* vanP;
Sports* sportsP;
Regular* regP;
for (int i=0;i<12;i++)
{
carP=arr[i];
if (typeid(*carP) == typeid(Van))
{
vanP=carP;
vanFile<<*vanP<<endl;
} else
if (typeid(*carP) == typeid(Sports))
{
sportsP=carP;
sportsFile<<*sportsP<<endl;
} else
if (typeid(*carP) == typeid(Regular))
{
regP=carP;
regularFile<<*regP<<endl;
}
}
vanFile.close();
sportsFile.close();
regularFile.close();
Remember, I said I wasn't a C++ programmer. This might be all
wrong.
thank you it was a good progress to me,but i had to add more , just for your knowlage
ofstream vanFile("c:/van.txt");
ofstream sportsFile("c:/sports.txt");
ofstream regularFile("c:/regular.txt");
Car* carP;
Van* vanP;
Sports* sportsP;
Regular* regP;
for (int i=0;i<12;i++)
{
carP=arr[i];
if (typeid(*carP) == typeid(Van))
{
vanP=(Van *)carP;
vanFile<<*vanP<<endl;
} else
if (typeid(*carP) == typeid(Sports))
{
sportsP=(Sports *)carP;
sportsFile<<*sportsP<<endl;
} else
if (typeid(*carP) == typeid(Regular))
{
regP=(Regular *)carP;
regularFile<<*regP<<endl;
}
}
vanFile.close();
sportsFile.close();
regularFile.close();
--------------------------------------------------------------------------------
Thinker
03-28-2002, 10:32 AM
Thank you for posting back that it helped. I really wasn't sure
about it. That is good to know about casting the object.