当前位置: 首页 > ops >正文

9.1C++——类中特殊的成员函数

文章目录

  • 一、思维导图
  • 二、编程练习
    • 1、代码
    • 2、现象

一、思维导图

在这里插入图片描述

二、编程练习

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

1、代码

#include <iostream>using namespace std;
class Per
{
private:string name; //姓名int age;  //年龄int *height;  //身高int *weight;  //体重
public://构造函数Per(string name="--",int age=0,int height=0,int weight=0):name(name),age(age),height(new int(height)),weight(new int(weight)){cout << "Per::构造函数" << endl;}//拷贝构造函数Per(const Per &other):name(other.name),age(other.age),height(new int(*(other.height))),weight(new int(*(other.weight))){cout << "Per::拷贝构造函数" << endl;}//析构函数~Per(){delete height;delete weight;height=nullptr;weight=nullptr;cout << "Per::析构函数" << endl;}
};
class Stu
{
private:double score; //成绩Per p1;
public://构造函数Stu(double score=0,string name="--",int age=0,int height=0,int weight=0):score(score),p1(name,age,height,weight){cout << "Stu::构造函数" << endl;}//拷贝构造函数Stu(const Stu &other):score(other.score),p1(other.p1){cout << "Stu::拷贝构造函数" << endl;}//析构函数~Stu(){cout << "Stu::析构函数" << endl;}
};
int main()
{Stu s1(88,"lihua",18,175,55);Stu S2(s1);return 0;
}

2、现象

在这里插入图片描述

http://www.xdnf.cn/news/19380.html

相关文章:

  • GitHub 热榜项目 - 日榜(2025-09-01)
  • Android面试指南(六)
  • 科学研究系统性思维的方法体系:数据收集模板
  • 【Docker】Docker的容器Container、镜像Image和卷Volume对比
  • JVM核心机制:类加载与内存结构详解
  • Axios与Ajax:现代Web请求大比拼
  • 彻底搞懂 C++ 中的 `typename`
  • datax将数据从starrocks迁移至starrocks
  • 拆解期货交易所:清算交收体系!
  • MySQL 8 窗口函数详解
  • 【LeetCode热题100道笔记+动画】单词拆分
  • 报错处理(1)激活conda环境后pip库不能安装到已经激活的这个环境
  • 小迪Web自用笔记23
  • 红帽企业 Linux 系统性能调优指南
  • mapstruct原理以及使用对比
  • nginx-realip问题解决方案
  • 算法面试题(上)
  • 前阿里专家揭秘:你对中国十大GEO专家的认知,99%都是错的
  • 吴恩达机器学习作业十二:协同过滤(电影推荐系统)
  • 使用 BayesFlow 通过神经网络简化贝叶斯推断(一)
  • 中医文化学习软件,传承国粹精华
  • 动态滑动窗口还搞不清?一文搞定动态滑动窗口 | 基础算法
  • Windows系统安装Git详细教程
  • 【Java后端】Spring Boot 全局域名替换
  • TCP实现线程池竞争任务
  • FPGA|Quartus II 中使用TCL文件进行引脚一键分配
  • 深入理解零拷贝:本地IO与网络IO的性能优化利器
  • Docker基本介绍
  • MySQL 慢查询 debug:索引没生效的三重陷阱
  • 深度学习框架与工具使用心得:从入门到实战优化