C++ DAY3

cpp 复制代码
#include <iostream>

using namespace std;
//Per 类
class Per
{
    string name ;
    double *high;
    double *weight;
public:
    //有参构造函数 初始化列表方法
    Per (string name, double h,double w):name(name),high(new double(h)),weight(new double(w))
    {
        cout << "有参构造函数" << endl;
    }
    //析构函数 释放指针
    ~Per()
    {
        delete high;
        delete weight;
        high =nullptr;
        weight =nullptr;
        cout << "析构函数" << endl;
    }
    //由外界调用输出结果
    void show()
    {
        cout << "姓名:" << name << "身高:" << *high << "体重:" << *weight << endl;
    }
};

//Stu 类
class Stu
{
    double score;
    Per p1;
public:
    // 有参构造函数 初始化列表法 并初始化p1
    Stu(string n,double h,double w,double score):p1(n,h,w),score(score)
    {
        cout << "有参构造函数" << endl;
    }
    // 析构函数
    ~Stu()
    {
        cout << "析构函数" << endl;
    }
    //外界调用输出
    void show ()
    {
        p1.show();
        cout << "成绩:" << score << endl;
    }

};
int main()
{
    Stu s1("zhangsan",178,70,89);
    s1.show();
    
    return 0;
}
相关推荐
apcipot_rain9 小时前
C++转Java日记(04)
java·开发语言·c++
NEFU_Jyj9 小时前
牛客周赛R168
算法
云和数据.ChenGuang10 小时前
LangGraph 异步 Node 的设计与写法实践
开发语言·人工智能·python·机器学习·php
WL学习笔记11 小时前
vector模拟实现
c++
蜗牛一步步往上爬13 小时前
第十六章 C++语言在深度学习中的应用
开发语言·c++·深度学习
张张123y15 小时前
DSH 插件调试避坑:client.js 设置面板、热重载与高频报错排查表
开发语言·javascript·人工智能·开源·ecmascript·后端开发·ai agent
Navigator_Z17 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
wo_jack17 小时前
C++ 入门笔记(十六):第16课 vector 模拟实现十问十答 —— 从三指针到迭代器失效
c++·后端·面试·stl·vector
D e re k ZH17 小时前
C++ 标准库算法全景指南
数据结构·c++·算法