11.30 作业

1,设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。

#include 

using namespace std;

class Per{
private:
    string name;
    int age;
    double *high;
    double *weight;
public:
    Per()
    {
        cout << "无参构造函数2"  << endl;
    }
    Per(string name,int age,double high,double weight):name(name),age(age),high(new double(high)),weight(new double(weight))
    {
        cout << "有参构造函数2" << endl;
    }
    ~Per(){
        cout << "析构函数2" << endl;
    }
    Per(const Per &other):name(other.name),age(other.age),high(new double(*(other.high))),weight(new double(*(other.weight)))
    {
        cout << "拷贝函数2" << endl;
    }
    void show1(){
        delete high;
        delete weight;
        high = NULL;
        weight = NULL;
        cout << name << endl;
        cout << age << endl;
        cout << high << endl;
        cout << weight << endl;
    }

};

class Stu{
private:
    double score;
    Per p1;
public:
    Stu()
    {
        cout << "无参构造函数1" << endl;
    }
    Stu(string name,int age,double high,double weight,double score):p1(name,age,*(new double(high)),*(new double(weight))),score(score)
    {
           cout << "有参构造函数1" << endl;
    }
    ~Stu(){
        cout << "析构函数1" << endl;
    }
    Stu(const Stu &other):p1(other.p1),score(other.score)
    {
        cout << "拷贝函数1" << endl;
    }
    void show(){
        p1.show1();
        cout << score << endl;
    }
};


int main()
{
    Stu s1;
    Stu s2("zhangsan",18,180.00,100.00,89.5);
    Stu s3(s2);
    s3.show();
    return 0;
}

结果:

11.30 作业_第1张图片

2,思维导图

11.30 作业_第2张图片

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