//当类中的成员是其他类对象时,我们称该成员为对象成员
//构造的顺序是:先调用对象成员的构造,再调用本类构造
//析构顺序与构造相反
#include<iostream>
#include<string>
using namespace std;
class Phone
{
public:
Phone(string name)
{
cout << "Phone构造函数的调用" << endl;
P_name = name;
}
~Phone()
{
cout << "Phone析构函数的调用" << endl;
}
string P_name;
};
class Person
{
public:
Person(string name, string P_name) :m_name(name), m_Pname(P_name)
{
cout << "Person构造函数的调用" << endl;
}
~Person()
{
cout << "Person析构函数的调用" << endl;
}
string m_name;
Phone m_Pname;
};
void test01()
{
//当类中的成员是其他类对象时,我们称该成员为对象成员
//构造的顺序是:先调用对象成员的构造,再调用本类构造
//析构顺序与构造相反
Person p("张三", "苹果15");
cout << p.m_name << "拿着" << p.m_Pname.P_name << endl;
}
int main()
{
test01();
return 0;
}