I am currently busy with inheritance and I'm working with a base class called Vehicle
and a subclass called Truck
. The subclass is inheriting from the base class. I am managing with the inheritance of the public members of Vehicle
but can't access the private member called top_speed
in the function void describe() const
.
I know that one can do this in code (provided below) to access from the base class but it seems like I'm missing something. In the comment section in the code my question is stated more clearly for you.
void Truck::describe() const : Vehicle()
/*I know this is a way-> : Vehicle()
to inherit from the Vehicle class but how
can I inherit the top_speed private member of Vehicle in the
void describe() const function of the Truck class?*/
{
cout << "This is a Truck with top speed of" << top_speed <<
"and load capacity of " << load_capacity << endl;
}
Where in my Vehicle
class it is:
class Vehicle
{
private:
double top_speed;
public:
//other public members
void describe() const;
};
and in the Truck class it is this:
class Truck: public Vehicle
{
private:
double load_capacity;
public:
//other public members
void describe() const;
};
To be even more clear I am getting this error:
error: 'double Vehicle::top_speed' is private
What can I do in the void Truck::describe() const
function to fix it?