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
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 .
All overloaded operators except assignment (operator=) are inherited by derived classes.
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.
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.
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);
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With