You are here:

C++/on inheritence

Advertisement


Question
i was asked the following question in an inetrview:
#include<stdio.h>
class A{
public:
void display(void){
 printf("called A\n");
}
};
class B: public A{
public:
void display(void){
 printf("called B\n");
}
};
class C:public B{

};

main(){
C c;
c.display();

}
question: the answer display "called B". so how do the comipler decide
which display funtion will be called and whether it is at compile time
or at run time.


Answer
Here the display () in B overrides the one inherited from A. Now C gets what B has got (as it doesn't have any overriding function).

This case is decided at compile time.

Try to study about virtual function to get more information about polymorphism in C++.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.