class Date { string month; int day, year; public: Date(string m, int d, int y) { month = m; day = d; year = y; } }; class Person { string name; Date dateOfBirth; public: Person(string name, string month, int day, int year) { //將month、day和year傳遞給 dateOfBirth建構函式 this->name = name; } };Person 建構函式接收形參 month、day 和 year,將它們傳遞給其 dateOfBirth 成員的 Date 建構函式。C++ 提供了一個特殊的表示法,稱為成員初始化列表,它允許類別建構函式將實參傳遞給成員物件的建構函式。
class Person { string name; Date dateOfBirth; public: Person(string name, string month, int day, int year):dateOfBirth (month, day, year) //成員初始化類別 { this->name = name; } };請注意,冒號在建構函式頭的末尾。另外,在呼叫被包含的 Date 物件的建構函式時,它使用的是物件的名稱即 dateOfBirth,而不是使用物件的類,即 Date。這允許在相同的初始化列表中呼叫同一個類的不同物件的建構函式。
class Date { string month; int day, year; public: Date(string m, int d, int y):month(m),day(d),year (y) // 成員初始化類別 { } }; class Person { string name; Date dateOfBirth; public: Person(string name, string month, int day, int year): name(name),dateOfBirth(month, day, year) { } };可以看到,Date 和 Person 建構函式的函數體現在是空的,這是因為,以前一般在函數體中執行的給成員變數賦值任務現在改由初始化列表完成。
class Country { string name; //其他欄位 };因為很多人都會“擁有”同一個國家,所以 Person 和 Country 之間的 Has-a 關係不應該通過在每個 Person 物件中嵌入一個 Country 類的範例來實現。
class Person { string name; Date dateOfBirth; shared_ptr<Country> pCountry; //指向居住的國家 public: Person(string name, string month, int day, int year, shared_ptr<Country>& pC) { } };
int Person :: uniquePersonID;
該靜態成員用於生成一個數位,在建立 Person 物件分配給它。這些數位可以作為一種通用的個人身份識別號碼,就像現實生活中的個人身份證號碼一樣。這些數位儲存在 Person 和 Date 類的 personID 欄位中,用於標識正在建立或銷毀的物件。每個 dateOfBirth 物件攜帶與包含它的 Person 物件相同的 personID 編號。// This program illustrates aggregation, composition and object lifetimes. #include <iostream> #include <string> using namespace std; class Date { string month; int day, year; int personID; // ID of person whose birthday this is public: Date(string m, int d, int y, int id):month(m), day(d), year(y), personID(id) { cout << "Date-Of-Birth object for person " << personID << " has been created.n"; } ~Date() { cout << "Date-Of-Birth object for person " << personID << " has been destroyed. n"; } }; class Country { string name; public: Country(string name) : name(name) { cout << "A Country object has been created.n"; } ~Country() { cout << "A Country obj ect has been destroyed.n"; } }; class Person { string name; Date dateOfBirth; int personID; // Person identification number (PID) shared_ptr <Country> pCountry; public: Person(string name, string month, int day,int year, shared_ptr<Country>& pC): name(name),dateOfBirth(month,day,year,Person::uniquePersonID), personID(Person::uniquePersonID), pCountry(pC) { cout << "Person object "<< personID << " has been created.n"; Person::uniquePersonID ++; } ~Person() { cout << "Person object " << personID << " has been destroyed. n"; } static int uniquePersonID; // Used to generate PIDs }; //Define the static class variable int Person::uniquePersonID = 1; int main() { // Create a Country object shared_ptr<Country> p_usa = make_shared<Country>("USA"); // Create a Person object shared_ptr<Person> p = make_shared<Person>("Peter Lee", "January", 1, 1985, p_usa); // Create another Person object shared_ptr<Person> p1 = make_shared<Person>("Eva Gustafson", "May", 15, 1992, p_usa); cout << "Now there are two people.n"; // Both person will go out of scope when main returns return 0; }程式輸出結果:
A Country object has been created.
Date-Of-Birth object for person 1 has been created.
Person object 1 has been created.
Date-Of-Birth object for person 2 has been created. Person object 2 has been created.
Now there are two people.
Person object 1 has been destroyed.
Date-Of-Birth object for person 1 has been destroyed. Now there is only one.
Person object 2 has been destroyed.
Date-Of-Birth object for person 2 has been destroyed.
A Country object has been destroyed.