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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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 <iostream>
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
View Answer
Explanation: 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 <iostream>
using namespace std;
class Base
{
public:
virtual void print() const = 0;
};
class DerivedOne : public Base
{
public:
void print() const
{
cout << "DerivedOne\n";
}
};
class DerivedTwo : public Base
{
public:
void print() const
{
cout << "DerivedTwo\n";
}
};
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
View Answer
Explanation: 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 <iostream>
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
View Answer
Explanation: 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
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct a
{
int count;
};
struct b
{
int* value;
};
struct c : public a, public b
{
};
int main()
{
c* p = new c;
p->value = 0;
cout << "Inherited";
return 0;
}
a) Inherited
b) Error
c) Runtime error
d) inherited
View Answer
Explanation: In this program, We apply the multiple inheritance to structure.
Output:
$ g++ mul2.cpp $ a.out Inherited
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Base1
{
protected:
int SampleDataOne;
public:
Base1()
{
SampleDataOne = 100;
}
~Base1()
{
}
int SampleFunctOne()
{
return SampleDataOne;
}
};
class Base2
{
protected:
int SampleDataTwo;
public:
Base2()
{
SampleDataTwo = 200;
}
~Base2()
{
}
int SampleFunctTwo()
{
return SampleDataTwo;
}
};
class Derived1 : public Base1, public Base2
{
int MyData;
public:
Derived1()
{
MyData = 300;
}
~Derived1()
{
}
int MyFunct()
{
return (MyData + SampleDataOne + SampleDataTwo);
}
};
int main()
{
Base1 SampleObjOne;
Base2 SampleObjTwo;
Derived1 SampleObjThree;
cout << SampleObjThree.Base1 :: SampleFunctOne() << endl;
cout << SampleObjThree.Base2 :: SampleFunctTwo() << endl;
return 0;
}
a) 100
b) 200
c) Both 100 & 200
d) 150
View Answer
Explanation: In this program, We are passing the values by using multiple inheritance and printing the derived values.
Output:
$ g++ mul4.cpp $ a.out 100 200
9. Which design patterns benefit from the multiple inheritances?
a) Adapter and observer pattern
b) Code pattern
c) Glue pattern
d) Star pattern
View Answer
Explanation: Adapter and observer patterns benefit from the multiple inheritances.
10. What are the things are inherited from the base class?
a) Constructor and its destructor
b) Operator=() members
c) Friends
d) All of the mentioned
View Answer
Explanation: These things can provide necessary information for the base class to make a logical decision.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Check Computer Science Books
- Check Programming Books
- Apply for Computer Science Internship
- Practice Programming MCQs