Examveda

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Base
{
    public:
    virtual void print() const = 0;
};
class DerivedOne : virtual public Base
{
    public:
    void print() const
    {
        cout << "1";
    }
};
class DerivedTwo : virtual public Base
{
    public:
    void print() const
    {
        cout << "2";
    }
};
class Multiple : public DerivedOne, DerivedTwo
{
    public:
    void print() const
    {
        DerivedTwo::print();
    }
};
int main()
{
    Multiple both;
    DerivedOne one;
    DerivedTwo two;
    Base *array[ 3 ];
    array[ 0 ] = &both;
    array[ 1 ] = &one;
    array[ 2 ] = &two;
    for ( int i = 0; i < 3; i++ )
    array[ i ] -> print();
    return 0;
}

A. 121

B. 212

C. 12

D. 215

Answer: Option B


Join The Discussion

Related Questions on C plus plus miscellaneous

What is the difference between '++i' and 'i++' in C++?

A. None of the above

B. They both have the same effect

C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it

D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it