大佬教程收集整理的这篇文章主要介绍了C++聚合和组合详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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,int year):dateOfBirth (month,day,year) //成员初始化类别 { this->name = name; } };请注意,冒号在构造函数头的末尾。另外,在调用被包含的 Date 对象的构造函数时,它使用的是对象的名称即 dateOfBirth,而不是使用对象的类,即 Date。这允许在相同的初始化列表中调用同一个类的不同对象的构造函数。
class Date { String month; int day,int y):month(m),day(d),year (y) // 成员初始化类别 { } }; class Person { String name; Date dateOfBirth; public: Person(String name,int year): name(Name),dateOfBirth(month,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,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 y,int id):month(m),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,shared_ptr<Country>& pC): name(Name),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.
以上是大佬教程为你收集整理的C++聚合和组合详解全部内容,希望文章能够帮你解决C++聚合和组合详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。