#include<iostream>
using namespace std;
#include<string>
class Phone
{
public:
Phone(string phone)
{
phonename = phone;
cout <<"释放" <<phonename << endl;
}
~Phone()
{
cout << "释放手机" << endl;
}
private:
string phonename;
};
class Person
{
public:
Person(int n, string pp) :name(n), p(pp) { cout << "构造人" << endl; }
~Person()
{
cout << "释放人" << endl;
}
private:
int name;
Phone p;
};
void test01()
{
Person p(1, "Iphone 13 pro max");
}
int main()
{
test01();
system("pause");
return 0;
}
可以看到,先执行成员的构造函数,再执行自身构造函数,
先执行自身析构函数,再执行成员的构造函数