adplus-dvertising
frame-decoration

Question

What is the output of this program?
    #include <iostream>
    using namespace std;
    class BaseClass 
    {
        public:
        virtual void myFunction()
        {
            cout << "1";
        }
    };
    class DerivedClass1 : public BaseClass 
    {
        public:
        void myFunction()
        {
            cout << "2";
        }
    };
   class DerivedClass2 : public DerivedClass1 
    {
        public:
        void myFunction()
        {
            cout << "3";
        }
    };
    int main()
    {
        BaseClass *p;
        BaseClass ob;
        DerivedClass1 derivedObject1;
        DerivedClass2 derivedObject2;
        p = &ob;
        p -> myFunction();
        p = &derivedObject1;
        p -> myFunction();
        p = &derivedObject2;
        p -> myFunction();
        return 0;
    }

a.

123

b.

12

c.

213

d.

321

Answer: (a).123

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of this program?