Suppose you are asked to define a method void pC x where x m

Suppose you are asked to define a method void p(C x) where x must be able to accept as input objects of two unrelated classes A and B. What relationship suffices to hold between classes A, B, and C? What is your solution if class C is unrelated to both A and B?

Solution

The relationship between class A, B and C is both A and B is child class of virtual base class C

The reason C is virtual because both the class A and B inherits the base class C. So there can be 2 instance of base class is present in object. to avoid that C needs to be declared as virtual.

If C is unrelated to A and B then the function parameter in function p() should be declared as vector. This we the function can accept any classes as parameter. For example

void p( const vector<C>& v );

 Suppose you are asked to define a method void p(C x) where x must be able to accept as input objects of two unrelated classes A and B. What relationship suffic

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site