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;
}
相关推荐
划破黑暗的第一缕曙光几秒前
[C++]:2.类和对象(上)
c++·类和对象
季明洵3 分钟前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
shandianchengzi7 分钟前
【小白向】错位排列|图文解释公考常见题目错位排列的递推式Dn=(n-1)(Dn-2+Dn-1)推导方式
笔记·算法·公考·递推·排列·考公
I_LPL8 分钟前
day26 代码随想录算法训练营 回溯专题5
算法·回溯·hot100·求职面试·n皇后·解数独
Yeats_Liao9 分钟前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
墨雪不会编程10 分钟前
C++之【深入理解Vector】三部曲最终章
开发语言·c++
cpp_250113 分钟前
P9586 「MXOI Round 2」游戏
数据结构·c++·算法·题解·洛谷
浅念-17 分钟前
C语言编译与链接全流程:从源码到可执行程序的幕后之旅
c语言·开发语言·数据结构·经验分享·笔记·学习·算法
kyle~19 分钟前
ROS2---QoS策略
c++·机器人·ros2
小宋102127 分钟前
Java 项目结构 vs Python 项目结构:如何快速搭一个可跑项目
java·开发语言·python