Inheritance
- 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;
}
-
View Hint View Answer Discuss in Forum
NA
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.
- What is the output of this program?
#include <iostream>
using namespace std;
class PolygonShape
{
protected:
int W, H;
public:
void set_values (int num1, int num2)
{
W = num1; H = num2;}
};
class outputA
{
public:
void output (int k);
};
void outputA::output (int k)
{
cout << k << endl;
}
class RectangleShape: public PolygonShape, public outputA
{
public:
int Area ()
{
return (W * H);
}
};
class triangleShape: public PolygonShape, public outputA
{
public:
int Area()
{
return (W * H / 2);
}
};
int main ()
{
RectangleShape RectObject;
triangleShape TrgObject;
RectObject.set_values (3, 4);
TrgObject.set_values (3, 4);
RectObject.output (RectObject.Area());
TrgObject.output (TrgObject.Area());
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
We are using the multiple inheritance to find the area of rectangle and triangle.
- Which of the following advantages we lose by using multiple inheritance?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
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.
- Which symbol is used to create multiple inheritance?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
For using multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.
- What is meant by multiple inheritance?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Multiple inheritance enables a derived class to inherit members from more than one parent.