Multiple Inheritance(C++ Programming ) Questions and Answers
Question 3.
1.
#include
2.
using namespace std;
3.
class Base1
4.
{
5.
protected:
6.
int SampleDataOne;
7.
public:
8.
Base1()
9.
{
10.
SampleDataOne = 100;
11.
}
12.
~Base1()
13.
{
14.
}
15.
int SampleFunctOne()
16.
{
17.
return SampleDataOne;
18.
}
19.
};
20.
class Base2
21.
{
22.
protected:
23.
int SampleDataTwo;
24.
public:
25.
Base2()
26.
{
27.
SampleDataTwo = 200;
28.
}
29.
~Base2()
30.
{
31.
}
32.
int SampleFunctTwo()
33.
{
34.
return SampleDataTwo;
35.
}
36.
};
37.
class Derived1 : public Base1, public Base2
38.
{
39.
int MyData;
40.
public:
41.
Derived1()
42.
{
43.
MyData = 300;
44.
}
45.
~Derived1()
46.
{
47.
}
48.
int MyFunct()
49.
{
50.
return (MyData + SampleDataOne + SampleDataTwo);
51.
}
52.
};
53.
int main()
54.
{
55.
Base1 SampleObjOne;
56.
Base2 SampleObjTwo;
57.
Derived1 SampleObjThree;
58.
cout
What is the output of this program?
1.
#include
2.
using namespace std;
3.
class Base1
4.
{
5.
protected:
6.
int SampleDataOne;
7.
public:
8.
Base1()
9.
{
10.
SampleDataOne = 100;
11.
}
12.
~Base1()
13.
{
14.
}
15.
int SampleFunctOne()
16.
{
17.
return SampleDataOne;
18.
}
19.
};
20.
class Base2
21.
{
22.
protected:
23.
int SampleDataTwo;
24.
public:
25.
Base2()
26.
{
27.
SampleDataTwo = 200;
28.
}
29.
~Base2()
30.
{
31.
}
32.
int SampleFunctTwo()
33.
{
34.
return SampleDataTwo;
35.
}
36.
};
37.
class Derived1 : public Base1, public Base2
38.
{
39.
int MyData;
40.
public:
41.
Derived1()
42.
{
43.
MyData = 300;
44.
}
45.
~Derived1()
46.
{
47.
}
48.
int MyFunct()
49.
{
50.
return (MyData + SampleDataOne + SampleDataTwo);
51.
}
52.
};
53.
int main()
54.
{
55.
Base1 SampleObjOne;
56.
Base2 SampleObjTwo;
57.
Derived1 SampleObjThree;
58.
cout
Explanation:-
Answer: Option C. -> Both a & bIn this program, We are passing the values by using multiple inheritance and printing the derived values.
Output:
$ g++ mul4.cpp
$ a.out
100
200
Explanation:-
Answer: Option A. -> InheritedIn this program, We apply the multiple inheritance to structure.
Output:
$ g++ mul2.cpp
$ a.out
Inherited
Question 5.
1.
#include
2.
using namespace std;
3.
class student
4.
{
5.
public:
6.
int rno , m1 , m2 ;
7.
void get()
8.
{
9.
rno = 15, m1 = 10, m2 = 10;
10.
}
11.
};
12.
class sports
13.
{
14.
public:
15.
int sm;
16.
void getsm()
17.
{
18.
sm = 10;
19.
}
20.
};
21.
class statement:public student,public sports
22.
{
23.
int tot,avg;
24.
public:
25.
void display()
26.
{
27.
tot = (m1 + m2 + sm);
28.
avg = tot / 3;
29.
cout
What is the output of this program?
1.
#include
2.
using namespace std;
3.
class student
4.
{
5.
public:
6.
int rno , m1 , m2 ;
7.
void get()
8.
{
9.
rno = 15, m1 = 10, m2 = 10;
10.
}
11.
};
12.
class sports
13.
{
14.
public:
15.
int sm;
16.
void getsm()
17.
{
18.
sm = 10;
19.
}
20.
};
21.
class statement:public student,public sports
22.
{
23.
int tot,avg;
24.
public:
25.
void display()
26.
{
27.
tot = (m1 + m2 + sm);
28.
avg = tot / 3;
29.
cout
Explanation:-
Answer: Option B. -> 3010In this program, We are calculating the total and average marks of a student by using multiple inheritance.
Output:
$ g++ mul1.cpp
$ a.out
3010
Explanation:-
Answer: Option C. -> ErrorIn this program, 'Base' is an ambiguous base of 'Multiple'. So it is producing an error. And this
program is a virtual base class.
Question 9.
1.
#include
2.
using namespace std;
3.
class polygon
4.
{
5.
protected:
6.
int width, height;
7.
public:
8.
void set_values (int a, int b)
9.
{
10.
width = a; height = b;}
11.
};
12.
class output1
13.
{
14.
public:
15.
void output (int i);
16.
};
17.
void output1::output (int i)
18.
{
19.
cout
What is the output of this program?
1.
#include
2.
using namespace std;
3.
class polygon
4.
{
5.
protected:
6.
int width, height;
7.
public:
8.
void set_values (int a, int b)
9.
{
10.
width = a; height = b;}
11.
};
12.
class output1
13.
{
14.
public:
15.
void output (int i);
16.
};
17.
void output1::output (int i)
18.
{
19.
cout
Explanation:-
Answer: Option C. -> ErrorAnswer:c
Explanation:We are using the multiple inheritance to find the area of rectangle and triangle.
Output:
$ g++ mul.cpp
$ a.out
20
10