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: