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;
}
相关推荐
潮汐退涨月冷风霜40 分钟前
数字图像处理(1)OpenCV C++ & Opencv Python显示图像和视频
c++·python·opencv
长河2 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
第七序章2 小时前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
仙俊红2 小时前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
Cyan_RA92 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
逆小舟4 小时前
【Linux】人事档案——用户及组管理
linux·c++
再见晴天*_*5 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08276 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge8 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘8 小时前
39.网络流入门
开发语言·网络·c++·算法