【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;
}
相关推荐
艾莉丝努力练剑21 分钟前
【C++:异常】C++ 异常处理完全指南:从理论到实践,深入理解栈展开与最佳实践
java·开发语言·c++·安全·c++11
快乐zbc6 小时前
【C++ 基础】:给定一个指针 p,你能判断它是否指向合法的对象吗?
c++
岁忧7 小时前
GoLang五种字符串拼接方式详解
开发语言·爬虫·golang
tyatyatya7 小时前
MATLAB基础数据类型教程:数值型/字符型/逻辑型/结构体/元胞数组全解析
开发语言·matlab
sulikey7 小时前
C++类和对象(下):初始化列表、static、友元、内部类等核心特性详解
c++·static·初始化列表·友元·匿名对象·内部类·编译器优化
心无旁骛~7 小时前
python多进程和多线程问题
开发语言·python
星云数灵7 小时前
使用Anaconda管理Python环境:安装与验证Pandas、NumPy、Matplotlib
开发语言·python·数据分析·pandas·教程·环境配置·anaconda
kaikaile19957 小时前
基于遗传算法的车辆路径问题(VRP)解决方案MATLAB实现
开发语言·人工智能·matlab
四问四不知8 小时前
Rust语言进阶(结构体)
开发语言·后端·rust
q***9948 小时前
index.php 和 php
开发语言·php