I know that virtual
functions are declared in base class and can be (don't have to be unless it's a pure virtual function) refined in a derived class. However, I don't understand the difference between redefining a virtual function and redefining a regular function. Looking at this example code:
class base {
public:
virtual int getAge(){
return 20;
}
int getId(){
return 11111;
}
};
class dri : public base{
public:
int getAge(){
return 30;
}
int getId(){
return 222222;
}
};
int main(){
dri d;
std:: cout << d.getAge() << std::endl;
std:: cout << d.getId() << std::endl;
return 0;
}
will output:
30
222222
in which case having the virtual
keyword there didn't make any difference. both functions were overwritten. So why is it needed?