Assume the following code:
class base {
public:
int getAge(){
return 20;
}
};
class dri : public base {
public:
int getAge(){
return 30;
}
};
int main(){
base *b = new dri();
std::cout << b->getAge() << std::endl;
return 0;
}
I know with the magic of polymorphism the above code is compiled and you will get 20
in your console, you can also override them using the virtual
keyword.
My question is, why and when would you use this? if you need an object of type dri
why won't you just do dri d;
or dri *d = new dri()
, since it already includes all the functionalities of base
.