11.9. Worked Problem: Inheritance And Whole-Part Relationships

This problem and its solution follow the patterns illustrated in the Actor 4 example. It also extends the class relationship problem presented at the end of the previous chapter. The problem establishes the variable names (class members and function parameters), and they must be used consistently by your solutions.

class Person
{
	private:
		string	name;
	public:
		Person(string n) : name(n) {}
		friend ostream& operator<<(ostream& out, Person& me)
		{
			out << me.name << endl;
			return out;
		}
};

class Project
{
	private:
		char	code;
	public:
		Project(char c) : code(c) {}
		friend ostream& operator<<(ostream& out, Project& me)
		{
			out << me.code << endl;
			return out;
		}
};

class Record
{
	private:
		double salary;
	public:
		Record(double s) : salary(s) {}
		friend ostream& operator<<(ostream& out, Record& me)
		{
			out << me.salary << endl;
			return out;
		}
}

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)
		{
			(a)___________________________________
			(b)___________________________________
			(c)___________________________________
			(d)___________________________________

			return out;
		}
};

Fill in the blanks to complete the Employee inserter function.

  1. Print the name
  2. Print the salary
  3. If it is safe to do so, print the project code
  4. Print the id

Relationship Problem Solution