Problem Solution: Inheritance And Whole/Part Relationships

class Employee : public Person					// Inheritance
{
	private:
		Record		myRecord;			// Composition
		Project*	myProject = nullptr;		// Aggregation
		int		id;

	public:
		Employee(string name, double s, int i)
			: Person(name), myRecord(s), id(i) {}

		friend ostream& operator<<(ostream& out, Employee& me)
		{
			out << (Person &)me << endl;
			out << me.myRecord << endl;
			if (me.myProject != nullptr) out << *me.myProject << endl;
			out << me.id << endl;

			return out;
		}
};

The Person, Project, and Recode member variables are private, so they cannot be accessed directly from the Employee class. The Employee class must use the public interfaces (i.e., the public member functions) of the superclass and the two part classes to access their private members. The exact formatting (e.g., spaces and new lines) is not logically significant.

  1. Inheritance: me is cast from an Employee to a Person so that out << (Person &)me matches the Person inserter function parameter list.
  2. Composition: calls the Record inserter function.
  3. Aggregation: if myProject is not null, then calls the Project inserter function (using a null pointer to call a function causes a runtime error).
  4. Prints the Employee id.