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;
}
相关推荐
@@永恒29 分钟前
map&set
c++
&白帝&30 分钟前
Java @PathVariable获取路径参数
java·开发语言·python
青红光硫化黑1 小时前
React基础之项目创建
开发语言·javascript·ecmascript
小鹏编程1 小时前
【C++教程】C++中的基本数据类型
开发语言·c++·教程·少儿编程
熊峰峰1 小时前
C++第十节:map和set的介绍与使用
开发语言·c++
Antonio9151 小时前
【网络编程】事件选择模型
网络·c++
Mr.NickJJ1 小时前
Swift系列02-Swift 数据类型系统与内存模型
开发语言·ios·swift
Goober Airy1 小时前
PHP:格式化JSON为PHP语法格式
开发语言·php
白水先森2 小时前
牵引线标注:让地图信息更清晰的ArcGIS Pro技巧
开发语言·javascript·经验分享·arcgis·arcgispro