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;
}
相关推荐
持力行5 小时前
从C struct到C++中的class
c语言·c++
meilindehuzi_a6 小时前
从零理解并实现 RAG:用 LangChain.js 构建语义检索问答系统
开发语言·javascript·langchain
风流 少年6 小时前
Julia
开发语言·julia
江畔柳前堤6 小时前
GO01-Go 语言与主流编程语言深度对比
开发语言·人工智能·后端·微服务·云原生·golang·go
:-)7 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
GIS阵地8 小时前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
yaoxin5211238 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
阿里嘎多学长9 小时前
2026-07-14 GitHub 热点项目精选
开发语言·程序员·github·代码托管
charlie11451419110 小时前
Cinux: 为大内核铺路
开发语言·c++·操作系统·现代c++
2501_9142459310 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式