C++:day3

思维导图

练习题

cpp 复制代码
#include <iostream>
using namespace std;

class Per
{
private:
    string name;
    int age;
    int *height;
    double weight;

public:
    Per()
    {
        cout << "Per::无参构造函数" << endl;
    }
    Per(string name, int age, int height, double weight) : name(name), age(age), height(new int(height)), weight(weight)
    {
        cout << "Per::有参构造函数" << endl;
    }
    ~Per()
    {
        cout << "Per::析构函数" << endl;
        delete height;
        height = nullptr;
    }
};

class Stu
{
private:
    double score;
    Per p1;

public:
    Stu()
    {
        cout << "Stu::无参构造函数" << endl;
    }
    Stu(double score, string name, int age, int height, double weight) : score(score), p1(name, age, height, weight)
    {
        cout << "Stu::有参构造函数" << endl;
    }
    ~Stu()
    {
        cout << "Stu::析构函数" << endl;
    }
    void show()
    {
        cout << score << endl;
    }
};
int main(int argc, char const *argv[])
{
    Stu p(98, "葛飞飞", 23, 179, 86);
    p.show();
    return 0;
}
相关推荐
坚定学代码14 分钟前
static在c++中的使用
开发语言·c++
hansang_IR27 分钟前
【题解】P5364 [SNOI2017] 礼物
c++·线性代数·算法
Tisfy32 分钟前
LeetCode 3871.统计范围内的逗号 II:每次算一个逗号
数学·算法·leetcode·题解
无小道34 分钟前
C/C++——tuple小记
c++·tuple
小O的算法实验室36 分钟前
IEEE TEVC,基于K-means与DQN辅助元启发式算法的多目标两栖无人艇调度模型
算法·kmeans·启发式算法
mmmmath_31 小时前
LeetCode.350.两个数组的交集II
算法
Ivanqhz1 小时前
图是“拓扑 + 类型 + 形状“的世界
算法·决策树·机器学习·php·集成学习
上官唐云1 小时前
2024届本科 | C++ Linux服务端开发求职:从嵌入式到底层服务端的技术之路(附开源项目+简历)
linux·c++·开源·求职招聘
云雀衔光1 小时前
Java Spring AI MCP:在 Spring 里把 AI 接上你的业务系统
java·开发语言·人工智能·后端·测试工具·spring
重生之小比特1 小时前
【Java SE】数据类型与变量
java·开发语言·python