10.9作业

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

using namespace std;

class Per{
private:
    string name;
    int    age;
    double* height;
    double* weight;
public:
    Per(string name, int age, double height, double weight);
    Per(Per &per);
    ~Per();
    void show();
};

class Stu{
private:
    double score;
    Per p;
public:
    Stu(double score,string name, int age, double height, double weight);
    Stu(Stu &stu);
    ~Stu();
    void show();
};

Per::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::Per(Per &per)
    :name(per.name),age(per.age),height(new double(*per.height)),weight(new double(*per.weight)){
    cout << "Per::拷贝构造函数" << endl;
}
Per::~Per(){
    delete height;
    delete weight;
    cout << "Per::析构函数" << endl;
}
void Per::show(){
    cout << "name = " << name << "\tage = " << age << "\theight = " << *height << "\tweight = " << *weight;
}
Stu::Stu(double score,string name, int age, double height, double weight)
    :score(score),p(name,age,height,weight){
    cout << "Stu::有参构造函数" << endl;
}
Stu::Stu(Stu &stu):score(stu.score),p(stu.p){
    cout << "Stu::拷贝构造函数" << endl;
}
Stu::~Stu(){
    cout << "Stu::析构函数" << endl;
}
void Stu::show(){
    p.show();
    cout << "\tscore = " << score << endl;
}

int main()
{
    Stu s(95.5,"zhang",18,175,120);
    s.show();
    Stu s1=s;
    s1.show();
    return 0;
}
相关推荐
我不是疯子是傻子4 分钟前
Qt 程序打包部署全攻略:从 windeployqt 到 Inno Setup 的静默安装与版本迭代实战
开发语言·qt
城管不管1 小时前
重生——第十次面试之开源中国一面挂
java·linux·开发语言·算法·面试·职场和发展·开源
霸道流氓气质1 小时前
Java开发者AI编程常用MCP、Skills、Rules全景汇总
java·开发语言·ai编程
三十岁老牛再出发1 小时前
08.18每日总结
c++·python·numpy·pandas
zoujiahui_20182 小时前
Python 包与环境管理工具 uv
开发语言·python·uv
吴声子夜歌2 小时前
Java面试题——JVM(二)
java·开发语言·jvm
CRMEB2 小时前
商城大促活动策划全流程:从定目标到复盘四阶段
java·开发语言·人工智能·ai·开源·php
denglinqings2 小时前
Java开发从入门到精通所有课程
java·开发语言
程序猿阿森2 小时前
Python 闭包与装饰器:从入门到精通
开发语言·python·面试
余额瞒着我当琳3 小时前
C++--vector底层,leetcode118杨辉三角实战,对于传统reserve的坑点,迭代器失效
开发语言·c++