Monday, 14 November 2011

Predict the output or error(s) for the following:

class base

{

public:


void baseFun(){ cout<<"from base"<<endl;}


};

class deri:public base

{

public:


void baseFun(){ cout<< "from derived"<<endl;}


};

void SomeFunc(base *baseObj)

{

baseObj->baseFun();


}

int main()

{

base baseObject;


SomeFunc(&baseObject);


deri deriObject;


SomeFunc(&deriObject);


}

Answer:

from base


from base


Explanation:

As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since a pointer to a derived class object is passed, it treats the argument only as a base class pointer and the corresponding base function is called.

1 comment: