I have a Car class that inherits a Vehicle class. Both the Car and Vehicle class takes in the parameter, 'wheels'. From my understanding of how inheritance works, the object Car would be constructed in two phases: Vehicle would construct first by calling its Constructor, followed by Car which would also call its constructor. My question is how would I write my Car's constructor when its parameters are being used by the Vehicle's constructor?
class Vehicle {
public:
Vehicle(int wheels);
};
class Car {
public:
Car(int wheels): Vehicle(wheels);
};
You need to inherit from Vehicle:
Header file:
class Car: public Vehicle {
public:
Car(int wheels);
};
Cpp file:
Car::Car(int wheels): Vehicle(wheels) {
}
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