Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class BaseClass
    {
    public:
    virtual void print() const = 0;
    };
    class DerivedClassA : virtual public BaseClass
    {
    public:
    void print() const
    {
    cout << " 5 ";
    }
    };
    class DerivedClassB : virtual public BaseClass
    {
    public:
    void print() const
    {
    cout << "3";
    }
    };
    class Multiple : public DerivedClassA, DerivedClassB
    {
    public:
    void print() const
    {
    DerivedClassB::print();
    }
    };
    int main()
    {
    Multiple both;
    DerivedClassA objectA;
    DerivedClassB objectB;
    BaseClass *array[ 3 ];
    array[ 0 ] = &both;
    array[ 1 ] = &objectA;
    array[ 2 ] = &objectB;
    for ( int K = 0; K < 3; K++ )
    array[ K ] -> print();
    return 0;
    }
    1. 5 3
    2. 3 5 3
    3. 3 3 5
    4. 5 3 3
    5. None of these
Correct Option: B

In this program, We are executing these based on the condition given in array. So it is printing as 5 3 5.



Your comments will be displayed only after manual approval.