简体   繁体   中英

C++, How do I call a Base classes' overloaded extraction operator in a derived class?

So this is a small part of a large assignment I have, I'm just unsure of the syntax for this.

I have a base class named Vehicle , which has these members: int fuelAmt and int fuelUsage )

(I am using namespace std )

I overloaded the << operator this way:

ostream& operator<<(ostream& osObject, const Vehicle& myVehicle)
{
    cout << "Fuel Usage Rate: " << myVehicle.fuelUsage << endl
         << "Fuel Amount:     " << myVehicle.fuelAmt << endl;

    return osObject;
}

I then call it this way:

cout << Vehicle;

The result is (example):

Fuel Usage Rate: 10;
Fuel Amount: 50;

I also have an Airplane class which derives from the Vehicle class, it introduces a new member: int numEngines .

How can I overload the << operator in the Airplane class, so that it will first call the "Vehicle overloaded operator results", and then the results of whatever I tell the << operator to print from the derived class... So, here's what I mean:

I need it to function like this in the Airplane class:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane)
{
        //First print the Fuel Usage rate and Fuel amount by calling
        //the Base class overloaded << function

         //then
        cout << "Number of Engines: " << myAirplane.numEngines << endl;

    return osObject;
}

How do I trigger the base class execution of outputting its members' values, in this derived class?

Is it something like changing the header? Like this:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane): operator<<Vehicle

How about the following:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane)
{
    osObject << static_cast<const Vehicle &>(myAirplane);
    osObject << "Number of Engines: " << myAirplane.numEngines << endl;

    return osObject;
}

Since the operator << is a nonmember function, you can't declare it virtual, which is ideally what you want. So you do the following

class Base
{
public:
    virtual std::ostream& output(std::ostream& out) const
    {
        return out << "Base";
    }
    virtual ~Base() {} //Let's not forget to have destructor virtual
};

class Derived : public Base
{
public:
    virtual std::ostream& output(std::ostream& out) const
    {
        Base::output(out); //<------------------------
        return out << "DerivedPart";
    }
    virtual ~Derived() {} //Let's not forget to have destructor virtual
};

and finally, have operator << for the base class only and the virtual dispatch will work its magic

std::ostream& operator << (std::ostream& out, const Base& b) 
{
    return b.output(out);
}
ostream& operator<<(ostream& osObject, const Airplane& myAirplane)
{
     //First print the Fuel Usage rate and Fuel amount by calling
     //the Base class overloaded << function
     cout << (Vehicle& ) myAirplane;

     //then
     cout << "Number of Engines: " << myAirplane.numEngines << endl;

     return osObject;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM