I have a class that have a attribute that its type is another class, and I would like to be able to set its value to be a instance of a child of this class.
I prefer to explain with a example:
// Some kinds of glasses
class Glass {
public:
virtual void punch() {
cout << "break" << endl;
}
}
class ArmoredGlass : public Glass {
public:
virtual void punch() {
cout << "nothing..." << endl;
}
}
// The main class
class Car {
public:
Glass glasses;
}
// main method
int main(void) {
Car car;
ArmoredGlass armoredGlass;
car.glasses = armoredGlass;
car.glasses.punch();
return 0;
}
I cannot change the value of car.glasses
to a Glass
child class, and that is what I would like to do.