17-【每天亿点点C++,简单又有趣儿】类对象作为成员对象 ------老千层饼了

注意:
构造与析构的顺序


#include<iostream>
#include<string>
using namespace std;

class Phone
{
public:
    Phone(string name)
    {
        pName = name;
        cout << "make phone" << endl;
    }
    ~Phone()
    {
        cout << "del phone" << endl;
    }
    string pName;
};


class Child
{
public:
    Child(string name,string pname):cName(name),p1(pname)
    {
        cout << "make Child" << endl;

    }
    ~Child()
    {
        cout << "del Child" << endl;

    }
    Phone p1;
    string cName;

};
/*

make phone
make Child
del Child
del phone
*/
void test_11()
{
    Child c("tom","apple");
    cout << "name = " << c.cName << "  phone = " << c.p1.pName << endl;
}
int main(){
    test_11();
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107591196
今日推荐