Logo 
Search:

C++ Programming FAQ

Submit Interview FAQ
Home » Interview FAQ » C++ ProgrammingRSS Feeds

C++ programming practical question 4

  Shared By: Adah Miller    Date: Jan 24    Category: C++ Programming    Views: 74

Answer:

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.

Share: 
 

Didn't find what you were looking for? Find more on C++ programming practical question 4 Or get search suggestion and latest updates.


Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: