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;
}
相关推荐
NiceCloud喜云3 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
小羊在睡觉3 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
cjhbachelor3 小时前
c++继承
c++
3DVisionary3 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
AI玫瑰助手4 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
好评笔记4 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466854 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
油炸自行车4 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋4 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒4 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先