If we derive class CB from class CA, which already has a method function CA::method, and then redefine the method, what will happen? (assuming that the method is not virtual in CA) will we get a redefinition error? or will the new definition replace that of the parent in the derived class?
#include <iostream>
usingnamespace std;
struct CA {
void method() { cout << "CA" << endl; }
};
struct CB : public CA {
void method() { cout << "CB" << endl; }
};
struct VA {
virtualvoid method() { cout << "VA" << endl; }
};
struct VB : public VA {
void method() { cout << "VB" << endl; }
};
int main() {
CA *ca = new CA;
CA *cb = new CB; //note: defined as CA*, not CB*
VA *va = new VA;
VA *vb = new VB; //note: defined as VA*, not VB*
ca->method();
cb->method();
va->method();
vb->method();
return 0;
}
Thank you very much for your good answer and example. Now I understand. So, it is possible to replcace the method in the parrent if we don't want to use polymorphism.