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;
}
相关推荐
清水白石0082 分钟前
React Native与Flutter长期维护成本对比
开发语言·数据库
幻智星科技6 分钟前
Karatsuba与CSD优化FPGA大数乘法
c++·fpga开发·密码学
麦序子6 分钟前
用 Python + python-docx 批量生成带表格样式的装修报价单 Word 文档(含合计公式)
开发语言·python·自然语言处理·word·python3.11
1-1=016 分钟前
Python PyCharm
开发语言·python·pycharm
人工智能培训18 分钟前
边缘AI与大模型轻量化:让智能跑进每一个终端
大数据·人工智能·算法·重构·生活
牛肉胡辣汤21 分钟前
Blender + Three.js 开发讲解:从 .blend 到能转的产品页
开发语言·javascript·blender
惊讶的猫22 分钟前
Spring AOP
java·开发语言
flower98032324 分钟前
C++消息总线软件开发框架AI重构版
c++·设计模式·架构
ii_best41 分钟前
按键精灵手机助手 v5.2.1 实测:inputText 逐字延时 + 调试面板多行复制,安卓脚本开发的两个输入痛点一次解决
android·开发语言·智能手机·自动化·ai编程·按键精灵