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...

The blank areas at the top, bottom, left, and right of a document are the ________

A) margins B) Decrease Decimal C) borders D) boundaries

Computer Science & Information Technology

The VB ________ is the window used for Microsoft Visual Basic for Applications

A) Editor B) Developer C) Subset D) Host

Computer Science & Information Technology