I have a C++ Base class with some derived classes. If I later extend the base class by inheritance, it there way to reflect the changes in the already derived classes. To make my question more clear, consider the following code.
#include <iostream>
class FourWheelVehicle
{
public:
FourWheelVehicle()
{std::cout<<"FourWheelVehicle created\n";}
};
class Car: public FourWheelVehicle
{
public:
Car()
{std::cout<<"Car created\n";}
};
class Toyota: public Car
{
public:
Toyota()
{std::cout<<"Toyota Car created\n";}
};
class Audi: public Car
{
public:
Audi()
{std::cout<<"Audi Car created\n";}
};
int main()
{
Toyota t;
std::cout<<"..............................\n";
Audi a;
return 0;
}
How can I extend FourWheelVehicle to FourWheelVehicleWithSeatBelt so that all the cars I derived earlier will get the seat belt without altering their code? I have tried the following:
FourWheelVehicle
/ \
/ \
/ \
Car \
/\ \
/ \ \
Toyota Audi FourWheelVehicleWithSeatBelt
class FourWheelVehicleWithSeatBelt: public FourWheelVehicle
{
public:
FourWheelVehicleWithSeatBelt()
{std::cout<<"FourWheelVehicleWithSeatBelt created\n";}
};
The above scheme does not seem to work!
Aucun commentaire:
Enregistrer un commentaire