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;
}
相关推荐
大熊背6 分钟前
树莓派相机自动白平衡详解(二)
算法·白平衡·isppipeline
醉城夜风~17 分钟前
(超详细)C++多态(Polymorphism)
开发语言·c++
这是个栗子20 分钟前
【JS代码分析】前端鉴权基础:Token 的本地存储与状态重置实践
开发语言·前端·javascript
Scabbards_24 分钟前
面试Leetcode - 算法合集
算法·leetcode·面试
chuan.bai29 分钟前
Java RAG 实战附录:qwen3 与 bge-m3 模型切换指南
java·人工智能·算法
wuyk55530 分钟前
7.AVL 树:第一个自平衡二叉搜索树
开发语言·stm32·单片机·算法
rannn_11134 分钟前
【力扣hot100】二叉树专题+总结
java·算法·leetcode·二叉树
啊嘞嘞?37 分钟前
力扣(岛屿数量)
算法·leetcode
小小龙学IT37 分钟前
现代 C++ 内存管理与资源安全深度解析:从 RAII 到所有权模型
c++·安全
SomeB1oody42 分钟前
【RustyML入门】5.3. 聚类指标
开发语言·后端·机器学习·rust·教程