c++学习第七天
创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。
提示:以下是本篇文章正文内容,下面案例可供参考。
一、const成员函数
//Date.h#pragma once#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1);//两个Print()可以同时存在,构成函数重载void Print() const;void Print();private:int _year;int _month;int _day;
};
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}//void Date::Print(const Date* this)
void Date::Print() const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}//void Date::Print(Date* this)
void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}
//Test.cpp#include"Date.h"int main()
{const Date d1(2025, 1, 1);d1.Print();Date d2(2025, 1, 2);d2.Print();
}
//Date.hbool operator<(const Date& d) const;bool operator==(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;bool operator!=(const Date& d) const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);Date operator-(int day) const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int operator-(const Date& d) const;
简单来说:只读函数可以加const,内部不涉及修改成员的都是只读函数
二、取地址及const取地址操作符重载
//Test.cpp#include"Date.h"int main()
{const Date d1(2025, 1, 2);d1.Print();cout << &d1 << endl;Date d2(2025, 1, 7);d2.Print();cout << &d2 << endl;return 0;
}
理论上上面的代码是不能运行的,因为自定义类型使用运算符需要重载。因为取地址及const取地址操作符重载这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
//自己写如下:
//日常使用自动生成的就可以了Date* operator&()
{return this;
}const Date* operator&() const
{return this;
}
// 不想被取到有效地址才自己写
Date* operator&()
{return nullptr;
}const Date* operator&() const
{return this;
}
三、流插入和流提取
//Date.hvoid operator<<(ostream& out);
//Date.cppvoid Date::operator<<(ostream& out)
{out << _year << "/" << _month << "/" << _day << endl;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);cout << d1 ;return 0;
}
原因:参数顺序不匹配
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);d1 << cout ;return 0;
}
不能写成成员函数,因为成员函数this指针永远占据第一个,所以只能往全局上写。
//Date.h#pragma once#include<iostream>
using namespace std;class Date
{// 友元声明//因为operator<<写在函数外面不能访问到_year,_month,_dayfriend void operator<<(ostream& out, const Date& d);Date(int year = 1, int month = 1, int day = 1);public:void Print();private:int _year;int _month;int _day;
};void operator<<(ostream& out, const Date& d);
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}void operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);Date d2(2024, 2, 2);cout << d1 ;return 0;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);Date d2(2024, 2, 2);cout << d1 << d2 ;return 0;
}
//Date.h#pragma once#include<iostream>
using namespace std;class Date
{friend ostream& operator<<(ostream& out, const Date& d);Date(int year = 1, int month = 1, int day = 1);public:void Print();private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}
//Date.h#pragma once#include<iostream>
using namespace std;class Date
{// 友元声明friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);Date(int year = 1, int month = 1, int day = 1);public:void Print();private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);//这里不能加const,因为这是在内部修改让外部拿到这个值
istream& operator>>(istream& in, Date& d);
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);Date d2(2024, 2, 2);cout << d1 << d2;cin >> d1;cout << d1;return 0;
}
四、再谈构造函数
1.初始化列表
class Date
{
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}private:int _year;int _month;int _day;
};
class A
{
public:A(int a = 0):_a(a){}
private:int _a;
};class Date
{
public:// 初始化列表是每个成员定义的地方// 不管你写不写,每个成员都要走初始化列表Date(int year, int month, int day, int& i): _year(year), _month(month),_a(1),_refi(i){// 赋值_day = day;}private:int _year; // 每个成员声明int _month = 1 ;int _day = 1;// C++11支持给缺省值,这个缺省值给初始化列表// 如果初始化列表没有显示给值,就用这个缺省值// 如果显示给值了,就不用这个缺省值//这里_day会使用缺省值,而_month不会使用// 下面三个例子必须定义时初始化const int _x = 10;int& _refi;A _a;
};
注意:
1. 每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)。
2.类中包含以下成员,必须放在初始化列表位置进行初始化:
①引用成员变量 ②const成员变量 ③自定义类型成员(且该类没有默认构造函数时)
4.成员变量在类中声明次序就是其在初始化列表中的初始化顺序,与其在初始化列表中的先后
总结:
能用初始化列表就用初始化初始化列表,但有些场景还是需要初始化列表和函数体混着用。
五、explicit关键字
#include<iostream>
using namespace std;class A
{
public:A(int i)//A(int i):_a(i){cout << "A(int i)" << endl;}private:int _a;
};int main()
{A aa1(1);// 单参数构造函数的隐式类型转换// 用2调用A构造函数生成一个临时对象,再用这个对象去拷贝构造aa2// 编译器会再优化,优化用2直接构造 A aa2 = 2;return 0;
}
A& ref1 = 2;
这里的问题原因是临时对象具有常性,ref不能引用,属于权限的放大,加上const就没有问题了。
const A& ref1 = 2;
#include<iostream>
using namespace std;class B
{
public:B(int b1, int b2)//explicit B(int b1, int b2):_b1(b1), _b2(b2){cout << "B(int b1, int b2)" << endl;}
private:int _b1;int _b2;
};int main()
{// C++11 支持多参数的隐式类型转换B bb1(1, 1);B bb2 = { 2, 2 };const B& ref2 = { 3,3 };return 0;
}
class B
{
public:explicit B(int b1, int b2):_b1(b1), _b2(b2){cout << "B(int b1, int b2)" << endl;}
private:int _b1;int _b2;
};
重点:
class A
{
public:explicit A(int i):_a(i){cout << "A(int i)" << endl;}private:int _a;
};class B
{
public:explicit B(int b1, int b2):_b1(b1), _b2(b2){cout << "B(int b1, int b2)" << endl;}
private:int _b1;int _b2;
};
六、生命周期
#include<iostream>
using namespace std;class A
{
public:explicit A(int i):_a(i){cout << "A(int i)" << endl;}A(const A& aa):_a(aa._a){cout << "A(const A& aa)" << endl;}~A(){cout << "~A()" << _a << endl;}
private:int _a;
};int main()
{// 有名对象 特点:生命周期在当前局部域A aa6(6);// 匿名对象。特点:生命周期只在这一行A(7);return 0;
}
总结
以上就是今天要讲的内容,本文仅仅简单介绍了c++的基础。
相关文章:

c++学习第七天
创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。 提示:以下是本篇文章正文内容,下面案例可供参考。 一、const成员函数 //Date.h#pragma once#include<iostream> using namespace std;class Date { public:Date…...

Ubuntu 24.04 LTS 通过 docker 安装 nextcloud 搭建个人网盘
准备 Ubuntu 24.04 LTSUbuntu 空闲硬盘挂载Ubuntu 安装 Docker DesktopUbuntu 24.04 LTS 安装 tailscale [我的Ubuntu服务器折腾集](https://blog.csdn.net/jh1513/article/details/145222679。 安装 nextcloud 参考 Ubuntu24.04系统Docker安装NextcloudOnlyoffice _。 更…...

RabbitMQ1-消息队列
目录 MQ的相关概念 什么是MQ 为什么要用MQ MQ的分类 MQ的选择 RabbitMQ RabbitMQ的概念 四大核心概念 RabbitMQ的核心部分 各个名词介绍 MQ的相关概念 什么是MQ MQ(message queue),从字面意思上看,本质是个队列,FIFO 先入先出&am…...

Open3D计算点云粗糙度(方法一)【2025最新版】
目录 一、Roughness二、代码实现三、结果展示博客长期更新,本文最近更新时间为:2025年1月18日。 一、Roughness 通过菜单栏的Tools > Other > Roughness找到该功能。 这个工具可以估计点云的“粗糙度”。 选择一个或几个点云,然后启动这个工具。 CloudCompare只会询问…...

算法6(力扣148)-排序链表
1、问题 给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 2、采用例子 输入:head [4,2,1,3] 输出:[1,2,3,4] 3、实现思路 将链表拆分成节点,存入数组使用sort排序,再用reduce重建链接 4、具…...

一文大白话讲清楚webpack基本使用——9——预加载之prefetch和preload以及webpackChunkName的使用
文章目录 一文大白话讲清楚webpack基本使用——9——预加载之prefetch和preload1. 建议按文章顺序从头看,一看到底,豁然开朗2. preload和prefetch的区别2. prefetch的使用3. preload的使用4. webpackChunkName 一文大白话讲清楚webpack基本使用——9——…...

【大数据2025】MapReduce
MapReduce 基础介绍 起源与发展:是 2004 年 10 月谷歌发表的 MAPREDUCE 论文的开源实现,最初用于大规模网页数据并行处理,现成为 Hadoop 核心子项目之一,是面向批处理的分布式计算框架。基本原理:分为 map 和 reduce …...

Windows图形界面(GUI)-QT-C/C++ - Qt List Widget详解与应用
公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 QListWidget概述 使用场景 常见样式 QListWidget属性设置 显示方式 (Display) 交互行为 (Interaction) 高级功能 (Advanced) QListWidget常见操作 内容处理 增加项目 删除项目…...
深度学习python基础(第二节) 分支语句和循环语句
本节主要介绍分支语句和循环语句的基本语法。 注意:在python中的作用域以缩进为准。有语言基础的很好理解,了解语法格式就可以。 布尔类型和比较运算符 # True真,False假 a True print(f"布尔变量a的内容是:{a},类型是:{type(a)}") 比较运算…...

Gin 源码概览 - 路由
本文基于gin 1.1 源码解读 https://github.com/gin-gonic/gin/archive/refs/tags/v1.1.zip 1. 注册路由 我们先来看一段gin代码,来看看最终得到的一颗路由树长啥样 func TestGinDocExp(t *testing.T) {engine : gin.Default()engine.GET("/api/user", f…...

第6章 ThreadGroup详细讲解(Java高并发编程详解:多线程与系统设计)
1.ThreadGroup 与 Thread 在Java程序中, 默认情况下, 新的线程都会被加入到main线程所在的group中, main线程的group名字同线程名。如同线程存在父子关系一样, Thread Group同样也存在父子关系。图6-1就很好地说明了父子thread、父…...
CentOS 7乱码问题如何解决?
1.使用超级用户操作: sudo su2.修改i18n配置文件: vi /etc/sysconfig/i18n将文件修改或添加为以下内容: LANG"zh_CN.UTF8" LC_ALL"zh_CN.UTF8"保存并退出(按Esc键,输入:wq,然后回车)…...
JavaScript语言的多线程编程
JavaScript语言的多线程编程 JavaScript是一种广泛使用的编程语言,主要用于网页开发。由于其单线程的特性,JavaScript 一直以来都有“无法进行多线程编程”的印象。尽管如此,随着技术的发展,JavaScript也逐渐引入了多线程的概念&…...

OpenSeaOtter使用手册-变更通知和持续部署
我们在OpenSeaOtter Server 0.1.1版本增加的镜像变更通知功能。通过镜像变更通知和OpenSeaOtter Agent就可以轻松获得持续部署能力。 镜像变更通知是通过push的方式下发到Agent的,Agent所在机器不需要外网地址。在Agent收到镜像变更通知后,就会调用对应的…...

(2)STM32 USB设备开发-USB虚拟串口
例程:STM32USBdevice: 基于STM32的USB设备例子程序 - Gitee.com 本篇为USB虚拟串口教程,没有知识,全是实操,按照步骤就能获得一个STM32的USB虚拟串口。本例子是在野火F103MINI开发板上验证的,如果代码中出现一些外设的…...

他把智能科技引入现代农业领域
江苏田倍丰农业科技有限公司(以下简称“田倍丰”)是一家专注于粮油种植的农业科技公司,为拥有300亩以上田地的大户提供全面的解决方案。田倍丰通过与当地政府合作,将土地承包给大户,并提供农资和技术,实现利…...

ingress-nginx代理tcp使其能外部访问mysql
一、helm部署mysql主从复制 helm repo add bitnami https://charts.bitnami.com/bitnami helm repo updatehelm pull bitnami/mysql 解压后编辑values.yaml文件,修改如下(storageclass已设置默认类) 117 ## param architecture MySQL archit…...

麒麟操作系统服务架构保姆级教程(十三)tomcat环境安装以及LNMT架构
如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情 之前咱们学习了LNMP架构,但是PHP对于技术来说确实是老掉牙了,PHP的市场占有量越来越少了,我认识一个10年的PHP开发工程师,十年工资从15k到今天的6k&am…...

亚博microros小车-原生ubuntu支持系列:4-手部检测
一 准备工作 在学习手部检测之前,有2个准备工作。 1 确保小车的摄像头能显示画面 参见:亚博microros小车-原生ubuntu支持系列:2-摄像头控制-CSDN博客 启动图传代理: docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm …...
关于回调函数(callback)
简介 在C中,回调函数是一种常见的编程技术,它允许你将一个函数作为参数传递给另一个函数,并在适当的时候调用它。回调函数通常用于事件处理、异步编程和模块化设计中。 1、函数指针:在C中,回调函数通常是通过函数指针…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器
——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的一体化测试平台,覆盖应用全生命周期测试需求,主要提供五大核心能力: 测试类型检测目标关键指标功能体验基…...

如何在看板中体现优先级变化
在看板中有效体现优先级变化的关键措施包括:采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中,设置任务排序规则尤其重要,因为它让看板视觉上直观地体…...
Linux离线(zip方式)安装docker
目录 基础信息操作系统信息docker信息 安装实例安装步骤示例 遇到的问题问题1:修改默认工作路径启动失败问题2 找不到对应组 基础信息 操作系统信息 OS版本:CentOS 7 64位 内核版本:3.10.0 相关命令: uname -rcat /etc/os-rele…...
MySQL 8.0 事务全面讲解
以下是一个结合两次回答的 MySQL 8.0 事务全面讲解,涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容,并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念(ACID) 事务是…...
MySQL 部分重点知识篇
一、数据库对象 1. 主键 定义 :主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 :确保数据的完整性,便于数据的查询和管理。 示例 :在学生信息表中,学号可以作为主键ÿ…...

Vue ③-生命周期 || 脚手架
生命周期 思考:什么时候可以发送初始化渲染请求?(越早越好) 什么时候可以开始操作dom?(至少dom得渲染出来) Vue生命周期: 一个Vue实例从 创建 到 销毁 的整个过程。 生命周期四个…...

tauri项目,如何在rust端读取电脑环境变量
如果想在前端通过调用来获取环境变量的值,可以通过标准的依赖: std::env::var(name).ok() 想在前端通过调用来获取,可以写一个command函数: #[tauri::command] pub fn get_env_var(name: String) -> Result<String, Stri…...
LCTF液晶可调谐滤波器在多光谱相机捕捉无人机目标检测中的作用
中达瑞和自2005年成立以来,一直在光谱成像领域深度钻研和发展,始终致力于研发高性能、高可靠性的光谱成像相机,为科研院校提供更优的产品和服务。在《低空背景下无人机目标的光谱特征研究及目标检测应用》这篇论文中提到中达瑞和 LCTF 作为多…...
用鸿蒙HarmonyOS5实现中国象棋小游戏的过程
下面是一个基于鸿蒙OS (HarmonyOS) 的中国象棋小游戏的实现代码。这个实现使用Java语言和鸿蒙的Ability框架。 1. 项目结构 /src/main/java/com/example/chinesechess/├── MainAbilitySlice.java // 主界面逻辑├── ChessView.java // 游戏视图和逻辑├──…...

从物理机到云原生:全面解析计算虚拟化技术的演进与应用
前言:我的虚拟化技术探索之旅 我最早接触"虚拟机"的概念是从Java开始的——JVM(Java Virtual Machine)让"一次编写,到处运行"成为可能。这个软件层面的虚拟化让我着迷,但直到后来接触VMware和Doc…...