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;
}
相关推荐
im_AMBER4 分钟前
Leetcode 162 除了自身以外数组的乘积 | 接雨水
开发语言·javascript·数据结构·算法·leetcode
是个西兰花11 分钟前
C++:异常
开发语言·c++·异常
cpp_250115 分钟前
P1873 [COCI 2011/2012 #5] EKO / 砍树
数据结构·c++·算法·题解·二分答案·洛谷·csp
AI人工智能+电脑小能手16 分钟前
【大白话说Java面试题】【Java基础篇】第18题:HashMap底层是如何扩容的
java·开发语言·面试·散列表·hash-index·hash
AbandonForce18 分钟前
Map类:pair键值对|map的基本操作|operator[]
开发语言·c++·算法·leetcode
澈20720 分钟前
C++核心:封装与static静态成员实战指南
开发语言·c++·算法
ShineWinsu33 分钟前
对于Linux:进程间通信IPC(匿名管道)的解析
linux·c++·面试·进程·通信·管道·ipc
wuyoula37 分钟前
全新多平台电商代付商城源码
开发语言·c++·ui·小程序·php源码
玖疯子38 分钟前
IT疑难杂症诊疗室:系统性故障排查指南
开发语言·php
码云数智-大飞39 分钟前
OpCache 原理深挖:从字节码缓存到预加载(Preloading)的实战配置
java·开发语言