Why the call of print() from pointer to base class (Class A) is working and call to print() from Child class object(Class C) is not working?
Statement 1: Will give "print A" as output as since A::print() is a virtual function it will call print() function of Class C which is inherited from A::print(). This will prove that Class C indeed has print() function
Statement 2: This will give compiler error as B::print(int x) will hide A::print().
Statement 3: Will give compilation error. Why?
Probably because print() is still hidden in Class C as Class C is also inheriting B::print(int x). If this is the case then why call from a->print() worked?
Another question: Is there any rule which specifies that B::print(int x) will hide A::print() in the child classes?
class A{
public:
virtual void print();
};
void A::print(){
cout<<"print A\n";
}
class B:public A{
public:
void print(int x);
};
void B::print(int x){
cout<<"print B " << x <<"\n";
}
class C:public B{
};
void funca(A *a){
a->print();//Statement 1
}
void funcb(B *b){
//b->print(); Statement 2
}
void funcc(C *c){
//c->print(); Statement 3
}
int main(){
C d;
funca(&d);
funcb(&d);
funcc(&d);
}