Subsumption and Dynamic Dispatch in C++

In Java a variables don't contain objects, only references to objects. This makes it easy to implement the subsumption rule because all references are the same size.

In C++ the situation is more complicated. The declaration:

Employee e;

declares a variable containing an Employee object. Attempting to squeeze a Manager object into e would probably require chopping off some of the Manager object's member variables.

C++ can imitate Java by declaring pointer variables:

Employee* e;

We can store a pointer to a Manager object in e:

e = new Manager();

Here is the C++ version of the Subsumption Rule:

A variable of type A* can hold a value of type B* if B <: A

By default, the Dynamic Dispatch Principle is false in C++. For example:

e->calcBonus();

invokes Employee::calcBonus, even if e holds a pointer to a Manager object. To enable dynamic dispatch, the programmer must explicitly declare calcBonus to be a virtual function:

class Employee: public Role {
public:
   virtual void calcBonus();
   // etc.
};

Note: it isn't necessary to declare calcBonus to be virtual in subtypes of Employee.