11月30日作业

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

#include 

using namespace std;

struct Per
{
private:
    string name;
    int age;
    double *height;
    double *weight;
public:
    Per(string name,int age,double height,double weight):name(name),age(age),height(new double(height)),weight(new double(weight))
    {
        cout << "Per::构造函数" << endl;
    }
    Per(const Per &Per1):name(Per1.name),age(Per1.age),height(new double(*(Per1.height))),weight(new double(*(Per1.weight)))
    {
        cout << "Per::拷贝构造函数" << endl;
    }
    ~Per()
    {
    	delete height;
        delete weight;
        cout << "Per::析构函数" << endl;
    }
};

struct Stu
{
private:
    double score;
    Per p1;
public:
    Stu(double score,string name,int age,double height,double weight):score(score),p1(name,age,height,weight)
    {
        cout <<  "Stu::构造函数" << endl;
    }
    Stu(const Stu &Stu1):score(Stu1.score),p1(Stu1.p1)
    {
        cout << "Stu::拷贝构造函数" << endl;
    }

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


int main()
{
    Stu s1(99,"小明",20,144,178);
    Stu s2(s1);
    Per p2("小红",19,120,166);
    Per p3(p2);
    return 0;
}

11月30日作业_第1张图片

思维导图
11月30日作业_第2张图片

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