10.11.1. Test Yourself: Inheritance And Whole-Part Relationships

The key to solving this problem is learning how to read and understand code, an important skill for a computer scientist. The problem follows the patterns illustrated in the Actor 3 example. Please notice that the problem establishes the variable names, both members and parameters, and you must use consistently in your solution.

class Person
{
	private:
		string	name;
	public:
		Person(string n) : name(n) {}
		void display() { cout << name << endl; }
};

class Project
{
	private:
		char	code;
	public:
		Project(char c) : code(c) {}
		void display() { cout << code << endl; }
};

class Record
{
	private:
		double salary;
	public:
		Record(double s) : salary(s) {}
		void display() { cout << salary << endl; }
}

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

	public:
		Employee(string name, double s, int i)
			: (a)_______________________________________{}

		~Employee(){ if (myProject != nullptr) (b)____________; }

		void set_project(char a_code)
		{
			(c)_________________________________________________;
		}

		void display() {(d)_______________________________}
};

Fill in the blanks to complete the Employee class.

  1. Complete the constructor initializer list for Employee. The constructor must call the constructors for Person and Record and initialize myProject
  2. When the program destroys an Employee object, make sure there isn't a memory leak.
  3. Write the body for set_project. Make sure that there are no memory leaks, and create a new Project with the given data.
  4. Write the body for the Employee display function. It must print the Employee's name, id, salary, and the Project's data.

Relationship Solution