6.5 作业

设计一个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):name(name),age(age),height(new double(height)),weight(new double(weight))
    {
        cout << "Per::构造类型" << endl;
    }
    ~Per()
    {
        delete height;
        delete weight;
        height=nullptr;
        weight=nullptr;
        cout << "Per::析构类型" << endl;

    }
    void show()
    {
        cout << name << " " << age << " " << *height << " " << *weight << endl;
    }
};
class Stu
{
private:
    double sroce;
    Per p1;
public:
    Stu(double sroce,string name,int age,double height,double weight):sroce(sroce),p1(name,age,height,weight)
    {
        cout << "Stu::构造类型" << endl;

    }
    ~Stu()
    {

        cout << "Stu::析构类型" << endl;

    }
    void show()
    {
        cout << sroce << " ";
        p1.show();
    }

};
int main()
{
    Stu s1(99.8,"lisi",18,175.6,140.3);
    s1.show();

    return 0;
}

思维导图:

相关推荐
不想写代码的星星12 小时前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
樱木Plus2 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_5 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星5 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛7 天前
delete又未完全delete
c++
端平入洛8 天前
auto有时不auto
c++
哇哈哈20219 天前
信号量和信号
linux·c++
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马9 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost