OOP - Week 6, Lecture 3: Multiple Inheritance
A Student Who Also Works
A university hires some of its students as part-time teaching assistants. Such a person is a part-time employee: they have hours, a rate and a pay. They are also a student: they have a student id and a gpa. Each of the two classes already exists in a simple form:
class PartTime : public Employee { // name, employee id, hours, rate, pay()
};
class Student { // student id, gpa
};
What class do we write for this person? Until now, a class had one base class. If TeachingAssistant is derived from PartTime only, it has no gpa, and we would copy the code of Student into it. If it is derived from Student only, it has no pay. We want both.
C++ allows it: a class can have more than one base class. This is called multiple inheritance.
1. Two Base Classes
Write the base classes after the :, separated by commas. Each one has its own type of inheritance:
class TeachingAssistant : public PartTime, public Student {
};
A TeachingAssistant is a PartTime and a Student. It has everything of both: pay() and getName() from the employee side, and getGpa() from the student side.
PartTime itself has a parent, Employee, but Student has none. The two bases have no parent in common, which keeps things simple. This lecture uses only this case.
2. Constructors and Destructors
The constructor of TeachingAssistant calls the constructor of each base class in the member initializer list, as in Lecture 2:
TeachingAssistant(const string& n, int employeeId, int h, double r, int studentId, double g)
: PartTime(n, employeeId, h, r), Student(studentId, g) {
}
The order follows the same rule as before: the parents are built before the child. With two parents, the order between them is the order in the class line, PartTime and then Student, and not the order in the initializer list. The compiler gives a warning if the list is written in a different order. Each parent is built after its own parents, so a TeachingAssistant is built like this:
Employee, the parent ofPartTimePartTimeStudentTeachingAssistant
Destructors run in the opposite order:
TeachingAssistantStudentPartTimeEmployee
3. When Both Bases Have the Same Name
Employee has a function getId(), and Student has one too. They are not the same function: one returns the employee id, and the other returns the student id. A TeachingAssistant inherits both, and the compiler cannot guess which one you mean:
TeachingAssistant ta("Essam", 1002, 20, 12.5, 20220923, 3.4);
cout << ta.getId(); // error: member 'getId' found in multiple base classes of different types
The exact words of the error depend on your compiler. The same happens with print(), which both sides have. The solution is to say which base you mean, with the class name and :::
cout << ta.PartTime::getId(); // the id of the employee
cout << ta.Student::getId(); // the id of the student
A name that exists in only one base, such as pay() or getGpa(), is used normally.
For print(), we can also write a new print in TeachingAssistant that prints both sides, using the same idea as Employee::print() in Lecture 1:
void TeachingAssistant::print() const {
PartTime::print();
Student::print();
}
Multiple inheritance is not used often. It is the right choice only when the new class really is both things, as here. When a class only needs to use the other class, keep it as a member instead (Week 4).
4. Putting It Together
This program creates one TeachingAssistant. The classes Employee and PartTime are the ones of Lecture 2, and the new ones are Student and TeachingAssistant. Click Changes since Lecture 2 to see exactly which lines are new. The constructors and the destructors print a line, so you can see the order. The calls that need a class name are the ones in main and in TeachingAssistant::print.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee {
private:
string name;
int id;
public:
Employee(const string& n, int i);
Employee(const Employee& other);
~Employee();
void setName(const string& n);
string getName() const;
void setId(int i);
int getId() const;
void print() const;
};
#endif#include <iostream>
#include "Employee.h"
using namespace std;
Employee::Employee(const string& n, int i) : name(n), id(0) {
setId(i);
cout << "Employee " << name << endl;
}
Employee::Employee(const Employee& other) : name(other.name), id(other.id) {
cout << "copy Employee " << name << endl;
}
Employee::~Employee() {
cout << "~Employee " << name << endl;
}
void Employee::setName(const string& n) {
name = n;
}
string Employee::getName() const {
return name;
}
void Employee::setId(int i) {
if (i > 0)
id = i;
}
int Employee::getId() const {
return id;
}
void Employee::print() const {
cout << "employee " << name << ", id " << id << endl;
}#ifndef PARTTIME_H
#define PARTTIME_H
#include "Employee.h"
class PartTime : public Employee {
private:
int hours;
double rate;
public:
PartTime(const string& n, int i, int h, double r);
~PartTime();
void setHours(int h);
void setRate(double r);
double pay() const;
void print() const;
};
#endif#include <iostream>
#include "PartTime.h"
using namespace std;
PartTime::PartTime(const string& n, int i, int h, double r) : Employee(n, i), hours(0), rate(0.0) {
setHours(h);
setRate(r);
cout << "PartTime " << getName() << endl;
}
PartTime::~PartTime() {
cout << "~PartTime " << getName() << endl;
}
void PartTime::setHours(int h) {
if (h >= 0)
hours = h;
}
void PartTime::setRate(double r) {
if (r >= 0.0)
rate = r;
}
double PartTime::pay() const {
return hours * rate;
}
void PartTime::print() const {
Employee::print();
cout << "part time, " << hours << " hours at " << rate << ", pay " << pay() << endl;
}#ifndef STUDENT_H
#define STUDENT_H
class Student {
private:
int id;
double gpa;
public:
Student(int i, double g);
~Student();
int getId() const;
double getGpa() const;
void print() const;
};
#endif#include <iostream>
#include "Student.h"
using namespace std;
Student::Student(int i, double g) : id(i), gpa(g) {
cout << "Student " << id << endl;
}
Student::~Student() {
cout << "~Student " << id << endl;
}
int Student::getId() const {
return id;
}
double Student::getGpa() const {
return gpa;
}
void Student::print() const {
cout << "student " << id << ", gpa " << gpa << endl;
}#ifndef TEACHINGASSISTANT_H
#define TEACHINGASSISTANT_H
#include "PartTime.h"
#include "Student.h"
class TeachingAssistant : public PartTime, public Student {
public:
TeachingAssistant(const string& n, int employeeId, int h, double r, int studentId, double g);
~TeachingAssistant();
void print() const;
};
#endif#include <iostream>
#include "TeachingAssistant.h"
using namespace std;
TeachingAssistant::TeachingAssistant(const string& n, int employeeId, int h, double r, int studentId, double g)
: PartTime(n, employeeId, h, r), Student(studentId, g) {
cout << "TeachingAssistant " << getName() << endl;
}
TeachingAssistant::~TeachingAssistant() {
cout << "~TeachingAssistant " << getName() << endl;
}
void TeachingAssistant::print() const {
PartTime::print();
Student::print();
}#include <iostream>
#include "TeachingAssistant.h"
using namespace std;
int main() {
TeachingAssistant ta("Essam", 1002, 20, 12.5, 20220923, 3.4);
ta.print();
cout << "employee id " << ta.PartTime::getId() << endl;
cout << "student id " << ta.Student::getId() << endl;
cout << "pay " << ta.pay() << endl;
cout << "end of main" << endl;
return 0;
} // ta is destroyed here#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee {
private:
string name;
int id;
public:
Employee(const string& n, int i);
Employee(const Employee& other);
~Employee();
void setName(const string& n);
string getName() const;
void setId(int i);
int getId() const;
void print() const;
};
#endif
#include <iostream>
#include "Employee.h"
using namespace std;
Employee::Employee(const string& n, int i) : name(n), id(0) {
setId(i);
cout << "Employee " << name << endl;
}
Employee::Employee(const Employee& other) : name(other.name), id(other.id) {
cout << "copy Employee " << name << endl;
}
Employee::~Employee() {
cout << "~Employee " << name << endl;
}
void Employee::setName(const string& n) {
name = n;
}
string Employee::getName() const {
return name;
}
void Employee::setId(int i) {
if (i > 0)
id = i;
}
int Employee::getId() const {
return id;
}
void Employee::print() const {
cout << "employee " << name << ", id " << id << endl;
}
#ifndef FULLTIME_H
#define FULLTIME_H
#include "Employee.h"
class FullTime : public Employee {
private:
double salary;
public:
FullTime(const string& n, int i, double s);
FullTime(const FullTime& other);
~FullTime();
void setSalary(double s);
double pay() const;
void print() const;
};
#endif
#include <iostream>
#include "FullTime.h"
using namespace std;
FullTime::FullTime(const string& n, int i, double s) : Employee(n, i), salary(0.0) {
setSalary(s);
cout << "FullTime " << getName() << endl;
}
FullTime::FullTime(const FullTime& other) : Employee(other), salary(other.salary) {
cout << "copy FullTime " << getName() << endl;
}
FullTime::~FullTime() {
cout << "~FullTime " << getName() << endl;
}
void FullTime::setSalary(double s) {
if (s >= 0.0)
salary = s;
}
double FullTime::pay() const {
return salary;
}
void FullTime::print() const {
Employee::print();
cout << "full time, pay " << pay() << endl;
}
#ifndef PARTTIME_H
#define PARTTIME_H
#include "Employee.h"
class PartTime : public Employee {
private:
int hours;
double rate;
public:
PartTime(const string& n, int i, int h, double r);
~PartTime();
void setHours(int h);
void setRate(double r);
double pay() const;
void print() const;
};
#endif
#include <iostream>
#include "PartTime.h"
using namespace std;
PartTime::PartTime(const string& n, int i, int h, double r) : Employee(n, i), hours(0), rate(0.0) {
setHours(h);
setRate(r);
cout << "PartTime " << getName() << endl;
}
PartTime::~PartTime() {
cout << "~PartTime " << getName() << endl;
}
void PartTime::setHours(int h) {
if (h >= 0)
hours = h;
}
void PartTime::setRate(double r) {
if (r >= 0.0)
rate = r;
}
double PartTime::pay() const {
return hours * rate;
}
void PartTime::print() const {
Employee::print();
cout << "part time, " << hours << " hours at " << rate << ", pay " << pay() << endl;
}
#ifndef MANAGER_H
#define MANAGER_H
#include "FullTime.h"
class Manager : public FullTime {
private:
int teamSize;
public:
Manager(const string& n, int i, double s, int t);
~Manager();
int getTeamSize() const;
};
#endif
#include <iostream>
#include "Manager.h"
using namespace std;
Manager::Manager(const string& n, int i, double s, int t) : FullTime(n, i, s), teamSize(t) {
cout << "Manager " << getName() << endl;
}
Manager::~Manager() {
cout << "~Manager " << getName() << endl;
}
int Manager::getTeamSize() const {
return teamSize;
}
#include <iostream>
#include "Manager.h"
#include "PartTime.h"
using namespace std;
int main() {
FullTime f("Omar", 1001, 3000);
FullTime g = f;
g.setName("Nada");
{
Manager m("Sara", 1003, 5000, 4);
} // m is destroyed here
PartTime p("layan", 1002, 20, 12.5);
cout << "end of main" << endl;
return 0;
} // p, g and f are destroyed here
Click each line of output to see what caused it.