C++ - Overloaded Operator in Derived Class not working

206 Views Asked by At

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!

1

There are 1 best solutions below

2
On

Add virtual function Output to the base class (at public or protected section):

virtual ostream & Output (ostream & out) const;

// ...

ostream & PocketMonster::Output (ostream & out) const
{
    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;
}

and override it in derived class:

virtual ostream & Output (ostream & out) const;

// ...

ostream & FireMonster::Output (ostream & out) const
{
    return out << PocketMonster::Output(out) << endl << "FireMonster Attributes:" << endl << "Temperature: " << p.temperature << endl;
}

Then rewrite operator<< for the base class in the following way:

ostream & operator << (ostream & out, const PocketMonster & p)
{
    return p.Output(out);
}

and remove operator<< for derived class.