Home » C++ Programming » Inheritance » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class BaseClass
    {
    public:
    virtual void print() const = 0;
    };
    class DerivedClassOne : public BaseClass
    {
    public:
    void print() const
    {
    cout << "Derived class One\n";
    }
    };
    class DerivedClassTwo : public BaseClass
    {
    public:
    void print() const
    {
    cout << "Derived class Two\n";
    }
    };
    class MultipleClass : public DerivedClassOne, public DerivedClassTwo
    {
    public:
    void print() const
    {
    DerivedClassTwo :: print();
    }
    };
    int main()
    {
    int i;
    MultipleClass both;
    DerivedClassOne one;
    DerivedClassTwo two;
    BaseClass *array[ 3 ];
    array[ 0 ] = &both;
    array[ 1 ] = &one;
    array[ 2 ] = &two;
    array[ i ] -> print();
    return 0;
    }
    1. Derived class Two
    2. Derived class One
    3. Compilation Error
    4. Runtime Error
    5. None of these
Correct Option: C

In this program, ‘BaseClass’ is an ambiguous base of ‘MultipleClass’. So it is producing an error. And this program is a virtual base class.



Your comments will be displayed only after manual approval.