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;
}
相关推荐
鹅毛在路上了2 小时前
C++, ffmpeg, libavcodec-RTSP拉流,opencv实时预览
c++·opencv·ffmpeg
John_ToDebug2 小时前
定制 ResourceBundle 的实现与 DuiLib 思想在 Chromium 架构下的应用解析
c++·chrome·ui
lingchen19062 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
gb42152872 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
一朵梨花压海棠go2 小时前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript
蒋星熠3 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
翻滚丷大头鱼3 小时前
Java 集合Collection—List
java·开发语言
小欣加油3 小时前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
王璐WL3 小时前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae3 小时前
C++ -- 模板
开发语言·c++·笔记·其他