Write a program that uses virtual base classes. The class at the top of the hierarchy should provide a constructor that takes at least one argument (i.e., do not provide a default constructor). What challenges does this present for the inheritance hierarchy?

What will be an ideal response?


If a virtual base class provides a constructor that requires arguments, the most de-
rived class must explicitly invoke the virtual base class’s constructor to initialize the
members inherited from the virtual base class. Implementing hierarchies with virtual base classes is simpler if default constructors are used for the base classes.

```
#include
using namespace std;

// class Base definition
class Base
{
public:
Base( int n )
{
num = n;
} // end Base constructor

void print()
{
cout << num;
} // end function print

private:
int num;
}; // end class Base

// class D1 definition
class D1 : virtual public Base
{
public:
D1(): Base( 3 ) {} // D1 constructor
}; // end class D1

// class D2 definition
class D2 : virtual public Base
{
public:
D2(): Base( 5 ) {} // D2 constructor
}; // end class D2

// class Multi definition
class Multi : public D1, D2
{
public:
Multi( int a ): Base( a ) {} // Multi constructor
}; // end class Multi

int main()
{
Multi m( 9 );
m.print();
cout << endl;
} // end main
```
9

Computer Science & Information Technology

You might also like to view...

Which of the following statements is true?

a. The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter value between the parentheses after the operation name. b. The UML indicates an operation’s return type by placing a colon and the return value after the parentheses following the operation name. c. UML class diagrams do not specify return types for operations that do not return values. d. Declaring instance variables public is known as data hiding or information hiding.

Computer Science & Information Technology

You can prevent hackers from capturing your personal information when using a Wi-Fi hotspot by utilizing a(n) ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

The ________ file format is considered an alternative to PDF

A) DOCX B) XLS C) RTF D) XPS

Computer Science & Information Technology

Where is the Template gallery located?

A) Formulas tab B) Data tab C) Backstage D) Insert tab

Computer Science & Information Technology