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;
}
相关推荐
ZhengEnCi5 小时前
2026年10月30日AI早报:Gemini 4 Carbon登顶、DeepSeek融资1055亿、智元机器人上岗实测
人工智能·算法
强哥聊算力5 小时前
《算力江湖·Intel帝国往事》第三集:安迪·格鲁夫的铁血时代:Intel为什么能把对手逼到墙角?
运维·人工智能·科技·算法·ai·gpu算力
步行cgn5 小时前
JSON.stringify 详解:把 JavaScript 对象变成 JSON 字符串
开发语言·javascript·json
Shaoshing5 小时前
LeetCode100 11. 盛最多水的容器
算法·leetcode
raindayinrain5 小时前
c++新特性--实用新特性
c++
Soneswy5 小时前
C++基础篇——类与对象(中)
开发语言·c++
CoderYanger5 小时前
Java EE 进阶:5. Spring IoC&DI(第二弹):Spring IoC
java·开发语言·spring boot·spring·面试·职场和发展·java-ee
骑着蜗牛撵大象3276 小时前
558 万美元训出 671B 模型:DeepSeek-V3 算法与系统的协同拆解
分布式·算法·moe·deepseek-v3·mla·fp8训练
Eyfcom6 小时前
〖阶段性回顾〗物流系统架构与算法的 Day 1-5 知识图谱与实战答疑
算法·系统架构·知识图谱·物流管理系统·快递管理系统
xunberg6 小时前
【Agent 生产化手记-02】Agent 怎么向人提问
人工智能·算法·架构