【无标题】

思维导图:

【无标题】_第1张图片

作业:

#include 
#include 

using namespace std;

class Per
{
private:
    string name;
    int age;
    int* height;
    int weight;

public:
    // 构造函数
    Per(string n, int a, int h, int w) : name(n), age(a), height(new int(h)), weight(w)
    {
        cout << "Per::有参构造函数" << endl;
    }

    // 拷贝构造函数
    Per(const Per &other) : name(other.name), age(other.age), height(new int(*(other.height))), weight(other.weight)
    {
        cout << "Per::拷贝构造函数" << endl;
    }

    // 析构函数
    ~Per()
    {
        cout << "Per::析构构造函数" << endl;
    }

    void show()
    {
        cout << name << endl;
        cout << age << endl;
        cout << *height << endl;
        cout << weight << endl;
    }
};

class Stu
{
private:
    int score;
    Per p1;

public:
    // 构造函数
    Stu(string n, int a, int h, int w, int s) : score(s), p1(n, a, h, w)
    {
       cout << "Stu::有参构造函数" << endl;
    }

    // 拷贝构造函数
    Stu(const Stu& other) : score(other.score), p1(other.p1)
    {
        cout << "Stu::拷贝构造函数" << endl;
    }

    // 析构函数
    ~Stu()
    {
        cout << "Stu::析构构造函数" << endl;
    }

    void show()
    {
        cout << score << endl;
        p1.show();
    }
};

int main() {
    Stu s1("John", 20, 175, 70, 85);
    Stu s2 = s1;

    s1.show();
    return 0;
}

运行结果:

【无标题】_第2张图片

你可能感兴趣的:(c++)