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;
}
相关推荐
1-1=01 小时前
C# 委托与事件
开发语言·c#
Fuyo_11192 小时前
C++ 运行时多态与虚函数机制全面剖析【面试重点】
开发语言·c++·面试
w_zero_one2 小时前
递归算法(一)
java·开发语言·算法
Julien20043 小时前
管理机密(一)
运维·服务器·开发语言·学习方法
Chen_harmony3 小时前
七、STL与string类
开发语言·c++
沐晓时光3 小时前
C语言入门,字符串和字符串函数知识讲解
c语言·开发语言
xxxiugou1234 小时前
C++命名空间与引用全解析
开发语言·c++
风影同志783244 小时前
20 个比特能装下多少行情?——实测拆解 Kronos 的 K 线分词器
算法
写的都是BUG4 小时前
APK 里的钥匙,一直就放在门口:聊聊加固真正该做什么
前端·算法
用户7624117567055 小时前
第十五章 人工智能基础
c++