当其他类对象作为本类成员,构造时候先构造类对象,再构造自身
析构的顺序与构造顺序相反
#include<iostream>
using namespace std;
#include<string>
class phone{
public:
phone(string pname){//手机品牌名,例如华为
m_pname=pname;
cout<<"phone的构造函数调用"<<endl;
}
string m_pname;
};
class person{
public:
person():m_name("张三"),m_phone("华为13"){
cout<<"person的构造函数调用"<<endl;
}
string m_name;
phone m_phone;
};
void test1(){
person p;
cout<<p.m_name<<"新买了"<<p.m_phone.m_pname<<endl;
}
int main(){
test1();
return 0;
}