C++学习day3

目录

作业:

[1> 思维导图](#1> 思维导图)

2>设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。

效果图:


作业:

1> 思维导图

2>设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。

cpp 复制代码
#include <iostream>

using namespace std;
//设计一个per类和stu类
class Per
{
private://私有的
    string name;//姓名
    int age;//年龄
    double *hight;//身高
    double *weight;//体重
public:
    //构造函数
    Per(string n,int a,double h,double w):name(n),age(a),hight(new double(h)),weight(new double(w))
    {
        cout << "Per::有参构造函数" << endl;

    }
    string GetName()const
    {
            return name;
        }

        int GetAge() const
        {
            return age;
        }

        double GetHight() const
        {
            return *hight;
        }

        double GetWeight() const
        {
            return *weight;
        }

    void show()
    {
        cout << "姓名:" << name << endl;
        cout << "年龄:" << age << endl;
        cout << "身高:" << *hight << endl;
        cout << "体重:" << *weight << endl;
    }
    //析构函数
    ~Per()
    {
        cout << "Per::析构函数" << endl;
        delete hight;
        delete weight;

    }


};
class Stu
{
private:
    int score;//成绩
     Per p1;//学生信息
public:
     //构造函数
     Stu(int s,string n,int a,double h,double w): score(s), p1(n,a,h,w)
     {
         cout << "Stu:: 有参构造函数" << endl;
     }
    void show()
    {
        cout << "成绩:"<< score << endl;
        p1.show();
    }
    //拷贝构造函数
    Stu(const Stu &other):score(other.score),p1(other.p1)
    {
        cout << "Stu::拷贝构造函数" <<endl;

    }
    Stu& operator=(const Stu& other)
    {
        if (this == &other)
            return *this;

        score = other.score;
        p1 = Per(other.p1.GetName(), other.p1.GetAge(), other.p1.GetHight(), other.p1.GetWeight());

        return *this;
    }


    //析构函数
    ~Stu()
    {
        cout << "Stu:: 析构函数" << endl;

    }

};

int main()
{
    Stu S1(100,"zhangsan",23,180.7,100.98);//有参构造函数
    S1.show();
    Stu S2(S1);//拷贝构造函数


    return 0;
}

效果图:

相关推荐
Monly211 小时前
Java:修改打包配置文件
java·开发语言
●VON1 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
我命由我123451 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
ZH15455891311 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
island13142 小时前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络
xcLeigh2 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh2 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
rainbow68892 小时前
EffectiveC++入门:四大习惯提升代码质量
c++
秋邱2 小时前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python
GHL2842710902 小时前
分析式AI学习
人工智能·学习·ai编程