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

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成员变量    ③自定义类型成员(且该类没有默认构造函数时)

   3. 尽量使用初始化列表初始化,因为不管你是否使用初始化列表,对于自定义类型成员变量,
一定会先使用初始化列表初始化。

   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;
};

重点:

        explicit修饰构造函数,禁止类型转换
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++学习第七天

创作过程中难免有不足&#xff0c;若您发现本文内容有误&#xff0c;恳请不吝赐教。 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考。 一、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)&#xff0c;从字面意思上看&#xff0c;本质是个队列&#xff0c;FIFO 先入先出&am…...

Open3D计算点云粗糙度(方法一)【2025最新版】

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

算法6(力扣148)-排序链表

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

一文大白话讲清楚webpack基本使用——9——预加载之prefetch和preload以及webpackChunkName的使用

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

【大数据2025】MapReduce

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

Windows图形界面(GUI)-QT-C/C++ - Qt List Widget详解与应用

公开视频 -> 链接点击跳转公开课程博客首页 -> ​​​链接点击跳转博客主页 目录 QListWidget概述 使用场景 常见样式 QListWidget属性设置 显示方式 (Display) 交互行为 (Interaction) 高级功能 (Advanced) QListWidget常见操作 内容处理 增加项目 删除项目…...

深度学习python基础(第二节) 分支语句和循环语句

本节主要介绍分支语句和循环语句的基本语法。 注意&#xff1a;在python中的作用域以缩进为准。有语言基础的很好理解&#xff0c;了解语法格式就可以。 布尔类型和比较运算符 # 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代码&#xff0c;来看看最终得到的一颗路由树长啥样 func TestGinDocExp(t *testing.T) {engine : gin.Default()engine.GET("/api/user", f…...

第6章 ThreadGroup详细讲解(Java高并发编程详解:多线程与系统设计)

1.ThreadGroup 与 Thread 在Java程序中&#xff0c; 默认情况下&#xff0c; 新的线程都会被加入到main线程所在的group中&#xff0c; main线程的group名字同线程名。如同线程存在父子关系一样&#xff0c; Thread Group同样也存在父子关系。图6-1就很好地说明了父子thread、父…...

CentOS 7乱码问题如何解决?

1.使用超级用户操作: sudo su2.修改i18n配置文件&#xff1a; vi /etc/sysconfig/i18n将文件修改或添加为以下内容&#xff1a; LANG"zh_CN.UTF8" LC_ALL"zh_CN.UTF8"保存并退出&#xff08;按Esc键&#xff0c;输入:wq&#xff0c;然后回车&#xff09…...

JavaScript语言的多线程编程

JavaScript语言的多线程编程 JavaScript是一种广泛使用的编程语言&#xff0c;主要用于网页开发。由于其单线程的特性&#xff0c;JavaScript 一直以来都有“无法进行多线程编程”的印象。尽管如此&#xff0c;随着技术的发展&#xff0c;JavaScript也逐渐引入了多线程的概念&…...

OpenSeaOtter使用手册-变更通知和持续部署

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

(2)STM32 USB设备开发-USB虚拟串口

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

他把智能科技引入现代农业领域

江苏田倍丰农业科技有限公司&#xff08;以下简称“田倍丰”&#xff09;是一家专注于粮油种植的农业科技公司&#xff0c;为拥有300亩以上田地的大户提供全面的解决方案。田倍丰通过与当地政府合作&#xff0c;将土地承包给大户&#xff0c;并提供农资和技术&#xff0c;实现利…...

ingress-nginx代理tcp使其能外部访问mysql

一、helm部署mysql主从复制 helm repo add bitnami https://charts.bitnami.com/bitnami helm repo updatehelm pull bitnami/mysql 解压后编辑values.yaml文件&#xff0c;修改如下&#xff08;storageclass已设置默认类&#xff09; 117 ## param architecture MySQL archit…...

麒麟操作系统服务架构保姆级教程(十三)tomcat环境安装以及LNMT架构

如果你想拥有你从未拥有过的东西&#xff0c;那么你必须去做你从未做过的事情 之前咱们学习了LNMP架构&#xff0c;但是PHP对于技术来说确实是老掉牙了&#xff0c;PHP的市场占有量越来越少了&#xff0c;我认识一个10年的PHP开发工程师&#xff0c;十年工资从15k到今天的6k&am…...

亚博microros小车-原生ubuntu支持系列:4-手部检测

一 准备工作 在学习手部检测之前&#xff0c;有2个准备工作。 1 确保小车的摄像头能显示画面 参见&#xff1a;亚博microros小车-原生ubuntu支持系列&#xff1a;2-摄像头控制-CSDN博客 启动图传代理&#xff1a; docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm …...

关于回调函数(callback)

简介 在C中&#xff0c;回调函数是一种常见的编程技术&#xff0c;它允许你将一个函数作为参数传递给另一个函数&#xff0c;并在适当的时候调用它。回调函数通常用于事件处理、异步编程和模块化设计中。 1、函数指针&#xff1a;在C中&#xff0c;回调函数通常是通过函数指针…...

centos 7 部署awstats 网站访问检测

一、基础环境准备&#xff08;两种安装方式都要做&#xff09; bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats&#xff0…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

【机器视觉】单目测距——运动结构恢复

ps&#xff1a;图是随便找的&#xff0c;为了凑个封面 前言 在前面对光流法进行进一步改进&#xff0c;希望将2D光流推广至3D场景流时&#xff0c;发现2D转3D过程中存在尺度歧义问题&#xff0c;需要补全摄像头拍摄图像中缺失的深度信息&#xff0c;否则解空间不收敛&#xf…...

Golang dig框架与GraphQL的完美结合

将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用&#xff0c;可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器&#xff0c;能够帮助开发者更好地管理复杂的依赖关系&#xff0c;而 GraphQL 则是一种用于 API 的查询语言&#xff0c;能够提…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!

一、引言 在数据驱动的背景下&#xff0c;知识图谱凭借其高效的信息组织能力&#xff0c;正逐步成为各行业应用的关键技术。本文聚焦 Spring Boot与Neo4j图数据库的技术结合&#xff0c;探讨知识图谱开发的实现细节&#xff0c;帮助读者掌握该技术栈在实际项目中的落地方法。 …...

如何更改默认 Crontab 编辑器 ?

在 Linux 领域中&#xff0c;crontab 是您可能经常遇到的一个术语。这个实用程序在类 unix 操作系统上可用&#xff0c;用于调度在预定义时间和间隔自动执行的任务。这对管理员和高级用户非常有益&#xff0c;允许他们自动执行各种系统任务。 编辑 Crontab 文件通常使用文本编…...

uniapp 小程序 学习(一)

利用Hbuilder 创建项目 运行到内置浏览器看效果 下载微信小程序 安装到Hbuilder 下载地址 &#xff1a;开发者工具默认安装 设置服务端口号 在Hbuilder中设置微信小程序 配置 找到运行设置&#xff0c;将微信开发者工具放入到Hbuilder中&#xff0c; 打开后出现 如下 bug 解…...

图解JavaScript原型:原型链及其分析 | JavaScript图解

​​ 忽略该图的细节&#xff08;如内存地址值没有用二进制&#xff09; 以下是对该图进一步的理解和总结 1. JS 对象概念的辨析 对象是什么&#xff1a;保存在堆中一块区域&#xff0c;同时在栈中有一块区域保存其在堆中的地址&#xff08;也就是我们通常说的该变量指向谁&…...

2025年低延迟业务DDoS防护全攻略:高可用架构与实战方案

一、延迟敏感行业面临的DDoS攻击新挑战 2025年&#xff0c;金融交易、实时竞技游戏、工业物联网等低延迟业务成为DDoS攻击的首要目标。攻击呈现三大特征&#xff1a; AI驱动的自适应攻击&#xff1a;攻击流量模拟真实用户行为&#xff0c;差异率低至0.5%&#xff0c;传统规则引…...