Okay, so the overloaded operator for the derived class is not working. It is only using the overloaded operator in the base class. Any ideas why?
Base class operator in class definition header file:
friend ostream & operator << (ostream & out, const PocketMonster & p);
Base class operator:
ostream & operator << (ostream & out, const PocketMonster & p)
{
out << endl << "(Monster Types: Type 1 = Fire, Type 2 = Water, Type 3 = Grass)" << endl
<< "PocketMonster Information: " << endl << "Name: " << p.name << endl
<< "Status (0=Dead, 1=Alive): " << p.status << endl << "Level: " << p.level << endl
<< "Strength: " << p.strength << endl
<< endl << "(Weapon Types: 1 = Fire, 2 = Water, 3 = Grass, 4 = Normal)" << endl
<< p.name << "'s Weapon Information: " << endl << "Weapon type: " << p.get_weaptype() << endl
<< "Weapon durability: " << p.get_weapdura() << endl << "Weapon level required: " << p.get_weaplvl() << endl << endl;
return out;
}
Derived class overloaded operator in class definition header file:
friend ostream & operator << (ostream & out, const FireMonster & p);
Derived class overloaded operator:
ostream & operator << (ostream & out, const FireMonster & p)
{
return out << static_cast<const PocketMonster&>(p) << endl << "FireMonster Attributes:" << endl << "Temperature: " << p.temperature << endl;
}
And here's a function where it tries to output the information
void displayLosers(vector<PocketMonster *> p)
{
for (int i=0; i<p.size(); i++)
{
if (p[i]->get_status() == false)
{
cout << p[i]->get_name() << " is a loser." << endl;
cout << *(p[i]);
}
}
}
Thanks for help in advance!
Add virtual function
Output
to the base class (at public or protected section):and override it in derived class:
Then rewrite
operator<<
for the base class in the following way:and remove
operator<<
for derived class.