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;
}
相关推荐
小小测试开发6 小时前
安装 Python 3.10+
开发语言·人工智能·python
KaMeidebaby6 小时前
卡梅德生物技术快报|PD1 单克隆抗体定制配套 N 糖全谱质控开发
前端·人工智能·算法·数据挖掘·数据分析
8Qi87 小时前
LeetCode 235. 二叉搜索树的最近公共祖先(LCA)
算法·leetcode·二叉树·递归·二叉搜索树·lca·迭代
好评1247 小时前
【C++】智能指针全解
c++·智能指针
AAA大运重卡何师傅(专跑国道)7 小时前
【无标题】
开发语言·c#
bIo7lyA8v7 小时前
算法稳定性分析中的随机扰动建模的技术8
算法
是阿建吖!7 小时前
【Linux】信号
android·linux·c语言·c++
城北徐宫7 小时前
Linux信号深度解剖:5种产生、3张表、4次切换
linux·c++·学习
liulilittle8 小时前
论 Linux 内核态全局稳态带宽的卡尔曼估计与工程实现
linux·服务器·网络·c++·计算机网络·tcp·通信
XBodhi.8 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio