Here is a listing of C++ programming questions on “Multiple Inheritance” along with answers, explanations and/or solutions:
1. What is meant by multiple inheritance?
a) Deriving a base class from derived class
b) Deriving a derived class from base class
c) Deriving a derived class from more than one base class
d) Deriving a derived base class
Answer: c
Clarification: Multiple inheritance enables a derived class to inherit members from more than one parent.
2. Which symbol is used to create multiple inheritances?
a) Dot
b) Comma
c) Dollar
d) star
Answer: b
Clarification: For using multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.
3. Which of the following advantages we lose by using multiple inheritances?
a) Dynamic binding
b) Polymorphism
c) Both Dynamic binding & Polymorphism
d) Constructor
Answer: c
Clarification: The benefit of dynamic binding and polymorphism is that they help making the code easier to extend but by multiple inheritance it makes harder to track.
4. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class polygon
-
{
-
protected:
-
int width, height;
-
public:
-
void set_values (int a, int b)
-
{
-
width = a; height = b;}
-
};
-
class output1
-
{
-
public:
-
void output (int i);
-
};
-
void output1::output (int i)
-
{
-
cout << i << endl;
-
}
-
class rectangle: public polygon, public output1
-
{
-
public:
-
int area ()
-
{
-
return (width * height);
-
}
-
};
-
class triangle: public polygon, public output1
-
{
-
public:
-
int area ()
-
{
-
return (width * height / 2);
-
}
-
};
-
int main ()
-
{
-
rectangle rect;
-
triangle trgl;
-
rect.set_values (4, 5);
-
trgl.set_values (4, 5);
-
rect.output (rect.area());
-
trgl.output (trgl.area());
-
return 0;
-
}
a) 20
b) 10
c)
20 10
d) 30
Answer: c
Clarification: We are using the multiple inheritance to find the area of rectangle and triangle.
Output:
$ g++ mul.cpp $ a.out 20 10
5. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class Base
-
{
-
public:
-
virtual void print() const = 0;
-
};
-
class DerivedOne : public Base
-
{
-
public:
-
void print() const
-
{
-
cout << "DerivedOnen";
-
}
-
};
-
class DerivedTwo : public Base
-
{
-
public:
-
void print() const
-
{
-
cout << "DerivedTwon";
-
}
-
};
-
class Multiple : public DerivedOne, public DerivedTwo
-
{
-
public:
-
void print() const
-
{
-
DerivedTwo :: print();
-
}
-
};
-
int main()
-
{
-
int i;
-
Multiple both;
-
DerivedOne one;
-
DerivedTwo two;
-
Base *array[ 3 ];
-
array[ 0 ] = &both;
-
array[ 1 ] = &one;
-
array[ 2 ] = &two;
-
array[ i ] -> print();
-
return 0;
-
}
a) DerivedOne
b) DerivedTwo
c) Error
d) DerivedThree
Answer: c
Clarification: In this program, ‘Base’ is an ambiguous base of ‘Multiple’. So it is producing an error. And this program is a virtual base class.
6. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class student
-
{
-
public:
-
int rno , m1 , m2 ;
-
void get()
-
{
-
rno = 15, m1 = 10, m2 = 10;
-
}
-
};
-
class sports
-
{
-
public:
-
int sm;
-
void getsm()
-
{
-
sm = 10;
-
}
-
};
-
class statement:public student,public sports
-
{
-
int tot,avg;
-
public:
-
void display()
-
{
-
tot = (m1 + m2 + sm);
-
avg = tot / 3;
-
cout << tot;
-
cout << avg;
-
}
-
};
-
int main()
-
{
-
statement obj;
-
obj.get();
-
obj.getsm();
-
obj.display();
-
}
a) 3100
b) 3010
c) 2010
d) 1010
Answer: b
Clarification: In this program, We are calculating the total and average marks of a student by using multiple inheritance.
Output:
$ g++ mul1.cpp $ a.out 3010