Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 366
Sergey Avatar asked Dec 08 '11 09:12

Sergey


People also ask

How do you access base class members in a derived class?

You can use both a structure and a class as base classes in the base list of a derived class declaration: If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private .

Are overloaded operators inherited in the derived class?

All overloaded operators except assignment (operator=) are inherited by derived classes.

Can we overload method in derived class?

In C#, just like in C++, there is no overload resolution between class Base and class Derived. Also, there is no overloading across scopes and derived class scopes are not an exception to this general rule.

What does a base class have access to in its derived class?

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Constructors, destructors and copy constructors of the base class.


2 Answers

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);
}
like image 58
Armen Tsirunyan Avatar answered Oct 14 '22 22:10

Armen Tsirunyan


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;
}
like image 39
Some programmer dude Avatar answered Oct 14 '22 22:10

Some programmer dude