【0823作业】C++:实现类嵌套,以及其构造函数、析构函数和拷贝构造函数

要求:

设计一个Per类。类中包含私有成员:姓名、年龄、指针成员身高、体重;

再设计一个Stu类,类中包含私有成员:成绩、Per类对象 p1;

设计这两个类的构造函数、析构函数和拷贝构造函数。

cpp 复制代码
#include <iostream>

using namespace std;
class Per
{
    string name; //姓名
    int age;     //年龄
    int *h;      //身高
    int *w;      //体重
public:
    Per():h(new int),w(new int)//在不传参的情况下,给指针成员h、w用堆区的空间初始化
    {
        cout << "Per无参构造" << endl;
    }
    Per(int h,int w):h(new int(h)),w(new int (w)) //在参数为整型变量的情况下,让h、w指向堆区申请的空间
    //但是把这片空间的内容,用传过来的整型变量h、w初始化
    {
        cout << "Per的有参构造" << endl;
    }
    //获取姓名赋值到结构体中
    void get_name(string name);
    //获取年龄赋值到结构体中
    void get_age(int age);

    //析构函数
    ~Per()
    {
        //在析构之前释放堆区空间
        cout << "准备释放空间:" << h << endl;
        cout << "准备释放空间:" << w << endl;
        delete h;
        delete w;
        h = nullptr;
        w = nullptr;
        cout << "Per的析构函数" << endl;
    }

    void show_Msg();  //输出信息

    //拷贝构造函数
    Per(Per &other)
    {
        this->name = other.name;
        this->age = other.age;
        h = new int;
        w = new int;
        *h = *(other.h);
        *w = *(other.w);
        cout << "Per的拷贝构造函数" << endl;
    }
};
class Stu
{
    float score;
    Per p1;
public:
    //void get_score();
    Stu()
    {
        cout << "Stu的无参构造函数" << endl;
    }
    Stu(float score,Per p1)
    {
        this->p1=p1;
        this->score = score;
        cout << "Stu的有参构造函数" << endl;
    }
    ~Stu()
    {
        //在析构之前释放堆区的空间
        cout << "Stu的析构函数" << endl;
    }
    Stu(Stu &other):score(other.score),p1(other.p1)
    {
        cout << "Stu的拷贝构造函数" << endl;
    }
    //输出信息
    void show_Stu_Msg();
};

//--------------------------------主函数------------------------------------
int main()
{
    Per s;          //无参构造
    Per s1(180,50); //有参构造,传递h和w的值
    s1.get_name("zhangsan"); //赋值
    s1.get_age(18);          //赋值
    s1.show_Msg();           //赋值

    Per s2 = s1;    //申请了一个Per的类对象,将s1的值初始化给s2
    s2.show_Msg();

    Stu p;         //无参构造
    Stu z1(98,s1); //有参构造
    z1.show_Stu_Msg(); //输出z1中的信息(float、Per p1)
    return 0;
}

//---------------------------------函数-------------------------------------
void Per::get_name(string name)//获取姓名赋值到结构体中
{
    this->name = name;
    cout << "姓名已保存: " << this->name << endl;
}

void Per::get_age(int age)    //获取年龄赋值到结构体中
{
    this->age = age;
    cout << "年龄已保存: " << this->age << endl;
}

void Per::show_Msg()//输出Per信息
{
    cout << "姓名:"<<name << " 年龄:"<<age << " 身高:"<<*h << " 体重:"<<*w << endl;
}

void Stu::show_Stu_Msg()//输出Stu信息
{
    p1.show_Msg();
    cout << "成绩为: " << score << endl;
}
相关推荐
史蒂芬_丁1 分钟前
Qt, C++数据类型扩展问题
数据库·c++·qt
6Hzlia10 分钟前
【Hot 100 刷题计划】 LeetCode 118. 杨辉三角 | C++ 动态规划题解
c++·leetcode·动态规划
三道渊37 分钟前
C语言:文件I/O
c语言·开发语言·数据结构·c++
hnlgzb40 分钟前
安卓app kotlin语法,Hilt是什么东西?
android·开发语言·kotlin
没用的阿_吉1 小时前
windows10 Qt5.15.14 msvc2019 编译部署
开发语言·qt
聊聊MES那点事1 小时前
JavaScript图表控件AG Charts使用教程:使用AG Charts React实时更新柱状图
开发语言·javascript·react.js·图表控件
ywf12151 小时前
Go基础之环境搭建
开发语言·后端·golang
潇冉沐晴1 小时前
DP——背包DP
算法·背包dp
是有头发的程序猿2 小时前
用Open Claw接口做1688选品、价格监控、货源对比
开发语言·c++·人工智能
逆境不可逃2 小时前
LeetCode 热题 100 之 543. 二叉树的直径 102. 二叉树的层序遍历 108. 将有序数组转换为二叉搜索树 98. 验证二叉搜索树
算法·leetcode·职场和发展