OOP - Week 4, Lecture 3: Composition
A Student Has a Birth Date and an Address
A student has a birth date and an address. We already have a Date class, with a day, a month and a year, and functions to read them. Without it, Student would have to store the date as separate values and write all of that again:
class Student {
private:
int id;
int birthDay, birthMonth, birthYear; // the same three values as Date
int building, street; // the address, also as separate values
};
It is better to reuse the classes we already have. A Student object can contain a Date object and an Address object as data members. This is called composition, and it describes a has-a relationship: a student has a birth date and has an address.
In this lecture we see how a class uses objects of other classes as its data members, how they are initialized, and in which order they are created and destroyed.
1. An Object as a Data Member
First, a small Address class with a building number and a street number:
class Address {
private:
int building, street;
public:
Address(int b, int s);
int getBuilding() const;
int getStreet() const;
};
Now Student uses Date and Address as the types of its data members, the same way it uses int:
class Student {
private:
int id;
Date birthDate; // a Date object inside the Student
Address address; // an Address object inside the Student
};
Each Student object has its own Date and its own Address inside it, just like it has its own id.
The members of Student are objects, so Student uses them like any other object: through their public functions. Date keeps day private, and the Student is outside Date, so it cannot read it:
class Student {
public:
void print() const {
cout << birthDate.getDay(); // works: getDay() is public
// cout << birthDate.day; // error: 'day' is a private member of 'Date'
}
};
getDay() is a const function, so print() can call it.
2. Initializing the Member Objects
When a Student is created, its birthDate and address have to be created too, and they need their constructors to run with the right values. We give those values in the member initializer list, from Lecture 2 of Week 2:
class Student {
public:
Student(int i, int d, int m, int y, int b, int s)
: id(i), birthDate(d, m, y), address(b, s) {
// the body can use birthDate and address: they already exist
}
};
birthDate(d, m, y) calls the constructor Date(int, int, int) for the member birthDate. address(b, s) does the same for address.
What if a member object is not in the initializer list? The compiler runs its default constructor. If the class has no default constructor, as with Date(int d, int m, int y) alone, the program does not compile:
class Student {
public:
Student(int i) { // error: constructor for 'Student' must explicitly
id = i; // initialize the member 'birthDate' which does not
} // have a default constructor
};
Writing birthDate = Date(1, 1, 2000); inside the body does not fix it. By then the member must already have been created. So for a member object, always use the initializer list.
The same rule applies when a Student is copied. The copy constructor that the compiler writes copies every member object with the copy constructor of that member. If you write your own copy constructor, a member object that is not in its initializer list is not copied: it is created with its default constructor. Suppose Address also has a default constructor, Address(), that sets both numbers to 0:
Student(const Student& other) : id(other.id), birthDate(other.birthDate) {
// address is not in the list: it is created by Address(), so it has 0 and 0
} // and it does not get the address of other
To copy it too, write address(other.address) in the list.
3. Order of Construction and Destruction
A Student is made of its members, so they must exist before the Student is complete. The body of the Student constructor may use them.
| ย | Order |
|---|---|
| Construction | 1. birthDate, 2. address, 3. the body of the Student constructor |
| Destruction | 1. the body of the Student destructor, 2. address, 3. birthDate |
- Member objects are built first, in the order they are declared in the class, and then the constructor body runs.
- Destruction is the reverse: the destructor body of
Studentruns first, then the members are destroyed from the last declared to the first.
4. Putting It Together
Three classes in one program: Date, Address, and Student, which has one of each. Every constructor and destructor prints a line, so you can see the order. To keep the example short the three classes are in one file. In a real project each class has its own .h and .cpp, as in Week 3.
#include <iostream>
using namespace std;
class Date {
private:
int day, month, year;
public:
Date(int d, int m, int y) {
day = d;
month = m;
year = y;
cout << "Date " << day << "/" << month << "/" << year << " created" << endl;
}
~Date() {
cout << "Date " << day << "/" << month << "/" << year << " destroyed" << endl;
}
int getDay() const {
return day;
}
int getMonth() const {
return month;
}
int getYear() const {
return year;
}
};
class Address {
private:
int building, street;
public:
Address(int b, int s) {
building = b;
street = s;
cout << "Address " << building << ", " << street << " created" << endl;
}
~Address() {
cout << "Address " << building << ", " << street << " destroyed" << endl;
}
int getBuilding() const {
return building;
}
int getStreet() const {
return street;
}
};
class Student {
private:
int id;
Date birthDate; // a Date object inside a Student
Address address; // an Address object inside a Student
public:
Student(int i, int d, int m, int y, int b, int s)
: id(i), birthDate(d, m, y), address(b, s) { // the member objects are built here
cout << "Student " << id << " created" << endl;
}
~Student() {
cout << "Student " << id << " destroyed" << endl;
}
void print() const {
cout << "id " << id
<< ", born " << birthDate.getDay() << "/" << birthDate.getMonth() << "/" << birthDate.getYear()
<< ", building " << address.getBuilding() << ", street " << address.getStreet() << endl;
}
};
int main() {
cout << "start of main" << endl;
{
Student s(101, 15, 3, 2005, 12, 5);
cout << "student ready" << endl;
s.print();
} // s is destroyed here
cout << "end of main" << endl;
return 0;
}Click each line of output to see what caused it.