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;
}
相关推荐
涛ing1 分钟前
【5. C++ 变量作用域及其深入探讨】
java·linux·c语言·开发语言·c++·ubuntu·vim
SY师弟24 分钟前
蓝桥杯单片机第七届省赛
c语言·c++·单片机·嵌入式硬件·职场和发展·蓝桥杯
Hi Man30 分钟前
Python之如何在Visual Studio Code 中写的python程序打包成可以在Windows系统下运行的.exe程序
开发语言·vscode·python
kcwqxx35 分钟前
day37|完全背包基础+leetcode 518.零钱兑换II ,377.组合总和II
c++·算法·leetcode·动态规划
CHANG_THE_WORLD1 小时前
C++并发编程指南04
开发语言·c++
轩情吖1 小时前
二叉树-堆(补充)
c语言·数据结构·c++·后端·二叉树··排序
powershell 与 api1 小时前
C#,shell32 + 调用控制面板项(.Cpl)实现“新建快捷方式对话框”(全网首发)
开发语言·windows·c#·.net
SomeB1oody1 小时前
【Rust自学】19.2. 高级trait:关联类型、默认泛型参数和运算符重载、完全限定语法、supertrait和newtype
开发语言·后端·rust
山茶花开时。2 小时前
[SAP ABAP] 静态断点的使用
开发语言·sap·abap
纠结哥_Shrek2 小时前
Java 有很多常用的库
java·开发语言