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;
}
相关推荐
码力码力我爱你9 分钟前
C HTML格式解析与生成之gumbo
c语言·开发语言·html
工程师老罗18 分钟前
Java笔试面试题AI答之设计模式(4)
java·开发语言·设计模式
KuaiKKyo22 分钟前
c++9月20日
java·c++·算法
muzi_liii23 分钟前
C++类和对象(下)
开发语言·c++
zero.cyx32 分钟前
JS函数部分
开发语言·前端·javascript
一丝晨光34 分钟前
语言的条件语句
java·开发语言·c++·程序员·c·条件语句·algol
细节的温柔42 分钟前
Python的主要特点及其应用领域
开发语言·python
Pdh胖大海44 分钟前
天源迪科java实习生面经
java·开发语言
咩咩大主教1 小时前
C++在Linux实现多线程和多进程的TCP服务器和客户端通信
linux·服务器·c语言·开发语言·c++·网络协议·tcp/ip
冰暮流星1 小时前
继承的例题
开发语言