Monday, 14 November 2011

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

class base

{

        public:


            virtual 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 derived


Explanation:

            Remember that baseFunc is a virtual function. That means that it supports run-time polymorphism. So the function corresponding to the derived class object is called.

1 comment: