8.23作业

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

#include 

using namespace std;

class Per
{
    string name;
    int age;
    float *sg;
    float *tz;
public:
    Per(string name,int age,float sg,float tz):name(name),age(age),sg(new float(sg)),tz(new float(tz))
    {
        cout << &sg << endl;
    }
    Per(Per &other)
    {
        name=other.name;
        age=other.age;
        sg=new float (*other.sg);
        tz=new float (*other.tz);
        cout << &sg << endl;
    }
    ~Per()
    {
        delete sg;
        delete tz;
    }
    void out()
    {
        cout << name << " " << age << " ";
        cout << *sg << " " << *tz << " ";
    }
};
class Stu
{
    int score;
    Per p1;
public:
    Stu(int score,string name,int age,float sg,float tz):score(score),p1(name,age,sg,tz)
    {

    }
    Stu(Stu &other):p1(other.p1)
    {
        score=other.score;
    }
    void out()
    {
        p1.out();
        cout << score << endl;
    }
};
int main()
{
    float a=180;
    float b=70;
    Stu s1(90,"zhangsan",20,a,b);
    Stu s2=s1;
    s1.out();
    s2.out();
    return 0;
}

8.23作业_第1张图片 

你可能感兴趣的:(c++,开发语言)