Google Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
This cannot a google interview question. This is basic rule of inheritance+ virtual function
if base class does not have virtual function ==-> function calling is based on type of pointer
and if the base class contain a virtual function then that function will be called on the base of content of the pointer.
So in this case the display is not virtual.. to the display of base class is called because you are accessing it via base class pointer.
try to run this code
#include <iostream>
using namespace std;
class Base {
public:
char* name;
virtual void display() {
cout << name << endl;
}
};
class Derived: public Base {
public:
char* name;
void display() {
cout << name << ", " << Base::name << endl;
}
};
int main() {
Derived d;
d.name = "Derived Class";
d.Base::name = "Base Class";
Derived* dptr = &d;
// standard conversion from Derived* to Base*
Base* bptr = dptr;
// call Base::display()
bptr->display();
}
in the code the display() drived class will be called as content of base pointer is a address of drived calls instance..that is the content of the pointer is a drived class.
Who are all answering should understand that it is not quality of question that matters, it is quality of answer you give, I know about Amazon/Google interviews where they have asked program to find prime number. Every Google interviewer has his own bar while interviewing. Some usually like to start with simple program like reverse a string first check how the candidate can think and see if we can code.Then only tougher questions and stronger discussions could be made.
So, rather than commenting on how simple questions it kindly, recommended to provide quality answers.
It is not about simplicity. It is about people using this site as domyhomework.com, and posting misleading the kind of questions they ask.
For instance, looking at this question one might assume Google is interested in specific programming language specifics (which is quite false) and trying to prepare for that can really deter from what you really need to prepare for: having a good grasp of the basics and being able to apply the basics, demonstrating problem solving skills.
Base::Display is non-virtual, which means that when you call it there isn't a v-table lookup, so it just calls based on the static type of the pointer (in this case Base).
- SirPentor December 15, 2012