Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class BaseClass
    {
    public:
    virtual void Function()
    {
    cout << "4";
    }
    };
    class DerivedClassA : public BaseClass
    {
    public:
    void Function()
    {
    cout << " 5";
    }
    };
    class DerivedClassB : public DerivedClassA
    {
    public:
    void Function()
    {
    cout << " 6";
    }
    };
    int main()
    {
    BaseClass *ptr;
    BaseClass BaseObject;
    DerivedClassA derivedObjectA;
    DerivedClassB derivedObjectB;
    ptr = &BaseObject;
    ptr -> Function();
    ptr = &derivedObjectA;
    ptr -> Function();
    ptr = &derivedObjectB;
    ptr -> Function();
    return 0;
    }
    1. 4 5 6
    2. 6 5 4
    3. 5 6 4
    4. 6 4 5
    5. None of these
Correct Option: A

We are passing the objects and executing them in a certain order and we are printing the program flow.



Your comments will be displayed only after manual approval.