【嵌入式学习】C++&QT-Day3-C++基础

笔记

见我的博客:https://lingjun.life/wiki/EmbeddedNote/19Cpp

作业

设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。

#include <iostream>

using namespace std;

class Per
{
   
private:
    string name;
    unsigned int age;
    float *height;
    float *weight;

public:
    // 构造函数
    Per(string n, unsigned int a, float h, float w) : name(n), age(a), height(new float(h)), weight(new float(w))
    {
   
        cout << "Per::有参构造函数" << endl;
    }

    // 析构函数
    ~Per()
    {
   
        delete height;
        delete weight;
        cout << "Per::析构函数" << endl;
    }

    // 拷贝构造函数
    Per(const Per &other) : name(other.name), age(other.age), height(new float(*(other.height))), weight(new float(*(other.weight)))
    {
   
        cout << "Per::拷贝构造函数" << endl;
    }
};

class Stu
{
   
private:
    int score;
    Per p1;

public:
    // 有参构造函数
    Stu(int score, string n, unsigned int a, float h, float w) : score(score), p1(n, a, h, w)
    {
   
        cout << "Stu::有参构造函数" << endl;
    }

    // 析构函数
    ~Stu()
    {
   
        cout << "Stu::析构函数" << endl;
    }

    // 拷贝构造函数
    Stu(const Stu &other) : score(other.score), p1(other.p1)
    {
   
        cout << "Stu::拷贝构造函数" << endl;
    }
};

int main()
{
   
    Stu s1(99, "张三", 18, 170, 60);
    Stu s2 = s1;
    return 0;
}

结果:
在这里插入图片描述

相关推荐

  1. 嵌入学习——C语言基础——day4

    2024-01-30 01:02:01       17 阅读
  2. 嵌入学习——C语言基础——day6

    2024-01-30 01:02:01       12 阅读
  3. 嵌入学习——C语言基础——day10

    2024-01-30 01:02:01       16 阅读
  4. 嵌入学习——C语言基础——day12

    2024-01-30 01:02:01       16 阅读
  5. 嵌入学习——C语言基础——day13

    2024-01-30 01:02:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-30 01:02:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-30 01:02:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-30 01:02:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-30 01:02:01       20 阅读

热门阅读

  1. 剑指offer面试题10 二进制中1的个数

    2024-01-30 01:02:01       40 阅读
  2. JVM 调优指南

    2024-01-30 01:02:01       32 阅读
  3. 三:C语言-输入与输出

    2024-01-30 01:02:01       30 阅读
  4. Kafka建立生产者消费者

    2024-01-30 01:02:01       29 阅读
  5. springboot 动态导出pdf

    2024-01-30 01:02:01       35 阅读
  6. leetcode 栈和队列相关题目

    2024-01-30 01:02:01       36 阅读
  7. 牛客周赛round30D题讲解(公式推导)

    2024-01-30 01:02:01       38 阅读
  8. 力扣0100——相同的树

    2024-01-30 01:02:01       39 阅读
  9. 代码随想录算法训练营|day15二叉树相关推荐

    2024-01-30 01:02:01       34 阅读
  10. 无人机组装的材料

    2024-01-30 01:02:01       34 阅读
  11. Sentinel背后的原理:守卫你的代码安全之旅

    2024-01-30 01:02:01       43 阅读