C++学习day4
作业:
1> 思维导图

2> 整理代码
1. 拷贝赋值函数课上代码
//拷贝赋值函数课上代码
#include<iostream>
using namespace std;//创建类
class Stu
{
private://私有的string name;int socer;int *age;//此处注意用到指针类型
public://共有的//无参构造函数(系统默认设置)Stu(){cout << "Stu:: 无参构造函数 " << endl;}//有参构造函数(自定义,自定义后需要将无参构造函数显性)Stu(string n,int s,int a):name(n),socer(s),age(new int (a))//初始化列表{cout << "Stu:: 有参构造函数" << endl;}//拷贝构造函数Stu(const Stu &other):name(other.name),socer(other.socer),age(new int(*other.age))//此处注意*other.age:因为定义是指针类型所以要去指针指向的内容进行赋值{cout << "Stu:: 拷贝构造函数" << endl;}//拷贝赋值函数Stu &operator=(const Stu &other) //此处不能用初始化列表因为是赋值操作{if(this != &other)//给自己赋值多此一举{name=other.name;socer=other.socer;age = new int (*other.age);//深拷贝赋值因为有指针防止段错误。。。}cout << "Stu:: 拷贝赋值函数" << endl;//注意返回值是自身引用return *this;}//析构函数~Stu()//会自动调用{cout << "Stu:: 析构函数" << endl;}};
int main()
{Stu s1;//无参构造函数Stu s2("王一博",100,23);//有参构造函数Stu s3(s2); //拷贝构造函数====》将s2拷贝给s3==》Stu s3=s2s1=s3;//拷贝赋值return 0;
}
2.匿名对象(用来初始化有名对象)课上代码
void fun(Stu g)//形参接收实参的值==》Stu g=Stu("lisa",120,25)
{}
int main()
{Stu s1;//无参构造函数Stu s2("王一博",100,23);//有参构造函数Stu s3(s2); //拷贝构造函数====》将s2拷贝给s3==》Stu s3=s2//用到匿名对象给有名对象初始化Stu s4=Stu("赵云",88,40);//给数组初始化Stu s[2]={Stu("哪吒",200,40),Stu("张飞",300,30)};//和c中初始化相似//匿名对象作为函数实参使用fun(Stu("lisa",120,25));s1=s3;//拷贝赋值return 0;
}
3.全局函数作友元,课上代码
//友元上课代码(全局函数作友元)
#include <iostream>
using namespace std;
//封装 房间类
class Room
{friend void goodfriend(Room &r);//关键字:friend
private://私有的string bedroom;//卧室
public://共有的string sittingroom;//客厅
public:Room(){//在类内赋值bedroom="卧室";sittingroom="客厅";}
};
//全局函数作友元
void goodfriend(Room &r)
{cout << "好朋友正在参观" << r.sittingroom << endl;//共有权限访问成功毋庸置疑cout << "好朋友正在参观" << r.bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
}int main()
{Room r;goodfriend(r);return 0;
}
4.成员函数作友元,课上代码
//成员函数作友元
#include <iostream>
using namespace std;
class Room;//需要声明一下,以免下面用到系统不知道报错
//封装好朋友类
class goodfriend
{
private:Room *r;//用到指针是因为指针的大小是固定的,而变量的大小不明,系统无法分配空间
public:goodfriend();//在类内声明void vist();//在类内声明
};//封装 房间类
class Room
{/*friend void goodfriend(Room &r);//关键字:friend*/friend void goodfriend::vist();
private://私有的string bedroom;//卧室
public://共有的string sittingroom;//客厅
public:Room(){//在类内赋值bedroom="卧室";sittingroom="客厅";}
};
//类外实现
goodfriend::goodfriend()
{r=new Room;//申请空间
}
void goodfriend::vist()
{cout << "好朋友正在参观" << r->sittingroom << endl;//共有权限访问成功毋庸置疑cout << "好朋友正在参观" << r->bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元//指针需要用->
}全局函数作友元
//void goodfriend(Room &r)
//{
// cout << "好朋友正在参观" << r.sittingroom << endl;//共有权限访问成功毋庸置疑
// cout << "好朋友正在参观" << r.bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
//}int main()
{Room r;//goodfriend(r);goodfriend g;g.vist();return 0;
}
5.课前热身
int const a; //......
int const *p; //......
int * const p; //
int const * const p; //
const int fun() {} //该函数是返回一个常整型变量
6.常成员函数&&非常成员函数
1.非常对象可以调用常成员函数,也可以调用非常成员函数,优先调用非常成员函数
2.常对象只能调用常成员函数,如果没有常成员函数,则报错
#include <iostream>
using namespace std;
class Stu
{
private:string name;//只对该成员变量在常成员函数中可以被修改mutableint id;
public:Stu(){}Stu(string name,int id):name(name),id(id){}//常成员函数void display()const //==》Stu const * const this;表示指针的值和指向都不可改{//this ->name ="li si";报错原因:因为被const修饰不可以更改cout << name << " " << id <<endl;}//非常成员函数void display()//==>Stu * const this;和常成员重载{this -> name ="lisi";cout << name << " " << id << endl;}};int main()
{cout << "Hello World!" << endl;const Stu s1("zhangsan",1001);//常对象s1.display();//常对象只能调用常成员函数return 0;
}
7.成员函数来实现运算符重载
//运算符重载
#include<iostream>
using namespace std;//构造类
class Club
{
private://私有的int age;int hight;
public://无参Club(){}Club(int a,int h):age(a),hight(h){}//成员函数实现算数运算符重载 加法const Club operator+(const Club &R)const//const 1:结果 2:右操作数 3:左操作数{//内部运算Club temp;temp.age=age+R.age;temp.hight=hight+R.hight;return temp;}//成员函数实现算数运算符重载 减法const Club operator-(const Club &R)const//const 1:结果 2:右操作数 3:左操作数{//内部运算Club temp;temp.age=age-R.age;temp.hight=hight-R.hight;return temp;}//成员函数实现算数运算符重载 乘法const Club operator*(const Club &R)const//const 1:结果 2:右操作数 3:左操作数{//内部运算Club temp;temp.age=age*R.age;temp.hight=hight*R.hight;return temp;}//成员函数实现算数运算符重载 除法const Club operator/(const Club &R)const//const 1:结果 2:右操作数 3:左操作数{//内部运算Club temp;temp.age=age/R.age;temp.hight=hight/R.hight;return temp;}//成员函数实现算数运算符重载 取余const Club operator%(const Club &R)const//const 1:结果 2:右操作数 3:左操作数{//内部运算Club temp;temp.age=age%R.age;temp.hight=hight%R.hight;return temp;}
关系运算符////成员函数实现关系运算符重载 >bool operator>(const Club &R)const{if(age>R.age && hight>R.hight){return true;}elsereturn false;}//成员函数实现关系运算符重载 <bool operator<(const Club &R)const{if(age<R.age && hight<R.hight){return true;}elsereturn false;}//成员函数实现关系运算符重载 >=bool operator>=(const Club &R)const{if(age>=R.age && hight>=R.hight){return true;}elsereturn false;}//成员函数实现关系运算符重载 <=bool operator<=(const Club &R)const{if(age<=R.age && hight<=R.hight){return true;}elsereturn false;}//成员函数实现关系运算符重载 ==bool operator==(const Club &R)const{if(age==R.age && hight==R.hight){return true;}elsereturn false;}//成员函数实现关系运算符重载 >bool operator!=(const Club &R)const{if(age!=R.age && hight!=R.hight){return true;}elsereturn false;}//赋值运算符//成员函数实现赋值运算符重载 +=Club &operator+=(const Club &R){age += R.age;hight += R.hight;return *this;//返回自身}//成员函数实现赋值运算符重载 -=Club &operator-=(const Club &R){age -= R.age;hight -= R.hight;return *this;//返回自身}//成员函数实现赋值运算符重载 =Club &operator=(const Club &R){age = R.age;hight = R.hight;return *this;//返回自身}//成员函数实现赋值运算符重载 *=Club &operator*=(const Club &R){age *= R.age;hight *= R.hight;return *this;//返回自身}//成员函数实现赋值运算符重载 /=Club &operator/=(const Club &R){age /= R.age;hight /= R.hight;return *this;//返回自身}//成员函数实现赋值运算符重载 %=Club &operator%=(const Club &R){age %= R.age;hight %= R.hight;return *this;//返回自身}void show(){cout << "age= " << age << " " << "hight= " << hight << endl;}
};
int main()
{Club c1(10,10);Club c2(10,10);Club c3=c1+c2;Club c4=c1-c2;Club c5=c1*c1;Club c6=c1/c2;Club c7=c1%c2;cout << "算数运算符" << endl;c3.show();c4.show();c5.show();c6.show();c7.show();cout << "关系运算符" << endl;if(c3!=c1){if(c3>c1){cout << "c3>c1 " << endl;}else if(c3<c1){cout << "c3<c1 " << endl;}cout << "c3!=c1" << endl;}else if(c3 == c1){if(c3>=c1){cout << "c3>=c1" << endl;}else if(c3<=c1){cout << "c3<=c1" << endl;}{cout << "c3==c1" << endl;}}cout << "赋值运算符" << endl;//赋值函数不要加类型否则属于初始化会报错c3+=c2;c4-=c2;c5*=c1;c6/=c2;c7%=c2;c3.show();c4.show();c5.show();c6.show();c7.show();return 0;
}
效果图:

8.全局函数实现运算符重载
//运算符重载
#include<iostream>
using namespace std;//构造类
class Club
{friend const Club operator+(const Club &L,const Club &R);friend const Club operator-(const Club &L,const Club &R);friend const Club operator*(const Club &L,const Club &R);friend const Club operator/(const Club &L,const Club &R);friend const Club operator%(const Club &L,const Club &R);friend bool operator>(const Club &L,const Club &R);friend bool operator<(const Club &L,const Club &R);friend bool operator<=(const Club &L,const Club &R);friend bool operator>=(const Club &L,const Club &R);friend bool operator==(const Club &L,const Club &R);friend bool operator!=(const Club &L,const Club &R);friend Club &operator+=( Club &L,const Club &R);friend Club &operator-=( Club &L,const Club &R);friend Club &operator*=( Club &L,const Club &R);friend Club &operator/=(Club &L,const Club &R);friend Club &operator%=( Club &L,const Club &R);private://私有的int age;int hight;
public://无参Club(){}Club(int a,int h):age(a),hight(h){}void show(){cout << "age= " << age << " " << "hight= " << hight << endl;}
};
//成员函数实现算数运算符重载 加法
const Club operator+(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{//内部运算Club temp;temp.age=L.age-R.age;temp.hight=L.hight-R.hight;return temp;
}//成员函数实现算数运算符重载 减法
const Club operator-(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{//内部运算Club temp;temp.age=L.age-R.age;temp.hight=L.hight-R.hight;return temp;
}//成员函数实现算数运算符重载 乘法
const Club operator*(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{//内部运算Club temp;temp.age=L.age*R.age;temp.hight=L.hight*R.hight;return temp;
}
//成员函数实现算数运算符重载 除法
const Club operator/(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{//内部运算Club temp;temp.age=L.age/R.age;temp.hight=L.hight/R.hight;return temp;
}
//成员函数实现算数运算符重载 取余
const Club operator%(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{//内部运算Club temp;temp.age=L.age%R.age;temp.hight=L.hight%R.hight;return temp;
}
关系运算符//
//成员函数实现关系运算符重载 >
bool operator>(const Club &L,const Club &R)
{if(L.age>R.age && L.hight>R.hight){return true;}elsereturn false;
}
//成员函数实现关系运算符重载 <
bool operator<(const Club &L,const Club &R)
{if(L.age>R.age && L.hight<R.hight){return true;}elsereturn false;
}
//成员函数实现关系运算符重载 >=
bool operator>=(const Club &L,const Club &R)
{if(L.age>=R.age && L.hight>R.hight){return true;}elsereturn false;
}
//成员函数实现关系运算符重载 <=
bool operator<=(const Club &L,const Club &R)
{if(L.age<=R.age && L.hight>R.hight){return true;}elsereturn false;
}
//成员函数实现关系运算符重载 ==
bool operator==(const Club &L,const Club &R)
{if(L.age==R.age && L.hight==R.hight){return true;}elsereturn false;
}//成员函数实现关系运算符重载 !=
bool operator!=(const Club &L,const Club &R)
{if(L.age!=R.age && L.hight>R.hight){return true;}elsereturn false;
}
//赋值运算符
//成员函数实现赋值运算符重载 +=
Club &operator+=( Club &L,const Club &R)
{L.age += R.age;L.hight += R.hight;return L;//返回自身
}
//成员函数实现赋值运算符重载 -=
Club &operator-=( Club &L,const Club &R)
{L.age -= R.age;L.hight -= R.hight;return L;//返回自身
}//成员函数实现赋值运算符重载 *=
Club &operator*=( Club &L,const Club &R)
{L.age *= R.age;L.hight *= R.hight;return L;//返回自身
}
//成员函数实现赋值运算符重载 /=
Club &operator/=( Club &L,const Club &R)
{L.age /= R.age;L.hight /= R.hight;return L;//返回自身
}
//成员函数实现赋值运算符重载 %=
Club &operator%=(Club &L,const Club &R)
{L.age /= R.age;L.hight /= R.hight;return L;//返回自身
}int main()
{Club c1(10,10);Club c2(10,10);Club c3=c1+c2;Club c4=c1-c2;Club c5=c1*c1;Club c6=c1/c2;Club c7=c1%c2;cout << "算数运算符" << endl;c3.show();c4.show();c5.show();c6.show();c7.show();cout << "关系运算符" << endl;if(c3!=c1){if(c3>c1){cout << "c3>c1 " << endl;}else if(c3<c1){cout << "c3<c1 " << endl;}cout << "c3!=c1" << endl;}else if(c3 == c1){if(c3>=c1){cout << "c3>=c1" << endl;}else if(c3<=c1){cout << "c3<=c1" << endl;}{cout << "c3==c1" << endl;}}cout << "赋值运算符" << endl;//赋值函数不要加类型否则属于初始化会报错c3+=c2;c4-=c2;c5*=c1;c6/=c2;c7%=c2;c3.show();c4.show();c5.show();c6.show();c7.show();return 0;
}
相关文章:
C++学习day4
作业: 1> 思维导图 2> 整理代码 1. 拷贝赋值函数课上代码 //拷贝赋值函数课上代码 #include<iostream> using namespace std;//创建类 class Stu { private://私有的string name;int socer;int *age;//此处注意用到指针类型 public://共有的//无参构…...
从零学算法54
54.给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。 螺旋遍历:从左上角开始,按照 向右、向下、向左、向上 的顺序 依次 提取元素,然后再进入内部一层重复相同的步骤,直到…...
Logback日志框架使用详解以及如何Springboot快速集成
Logback简介 日志系统是用于记录程序的运行过程中产生的运行信息、异常信息等,一般有8个级别,从低到高为All < Trace < Debug < Info < Warn < Error < Fatal < OFF off 最高等级,用于关闭所有日志记录fatal 指出每个…...
Nginx概念
Nginx概念 Nginx 是一款面向性能设计的 HTTP 服务器,相较于 Apache、lighttpd 具有占有内存少,稳定性高等优势,同时也是一个非常高效的反向代理、负载平衡服务器 nginx使用的是反应器模式,主事件循环等待操作系统发出准备事件的信…...
vim基础指令(自用)
这个是自己随便写的,类似于笔记 vim 多模式编辑器 查看指令: gg: 定位光标到最开始行 shift(按)g 定位到最结尾行 nshift(按)g 定位到任意行 shift$ 定位到本行结尾 0 定位到本行开头 w:跨单词移动 h.j.k,l: 左下上右 …...
【centos7安装ElasticSearch】
概述 最近工作中有用到ES ,当然少不了自己装一个服务器捣鼓。本文的ElasticSearch 的版本: 7.17.3 一、下载 ElasticSearch 点此下载 下载完成后上传至 Linux 服务器,本文演示放在: /root/ 下,进行解压࿱…...
ElementPlus Switch 开关基础使用
昨天开发用到开关组件 后台返回字段是 can_write 默认是0 or 1 但是Switch 组件绑定的默认值默认是 true or false 直接绑定会导致默认是关闭状态 在页面一加载 值发生变化时 会自己调用 查了文档 需要使用 active-value 和 inactive-value 来指定绑定的数据类型 …...
Spring Boot:自定义注解--annotation
目录 自定义注解的定义和作用范围如何创建自定义注解创建注解接口 如何使用自定义注解进行数据验证创建注解处理器控制器中使用注解 如何为字段添加注解 自定义注解的定义和作用范围 自定义注解可以作用在类、方法、属性、参数、异常、字段或其他注解上。 如何创建自定义注解…...
WIFI频段
问题: 5ghzwifi频段带宽怎么选 智造喵回答: 选择5GHz WiFi频段的带宽取决于你的网络需求和设备兼容性。通常有两种可选的带宽:20MHz和40MHz。 20MHz带宽:这是较为常见的 WiFi 频段带宽设置。它提供较好的穿墙能力和信号稳定性…...
Java的引用详解与示例
引用的作用 在Java中,引用(Reference)是一种重要的概念,它们用于管理对象的生命周期、内存分配和垃圾回收。引用的作用包括以下几个方面: 内存管理:引用帮助Java虚拟机(JVM)管理内存…...
c++视觉处理---霍夫变换
霍夫直线变换的函数 HoughLines 是OpenCV库中用于执行霍夫直线变换的函数。霍夫直线变换用于检测图像中的直线。下面是该函数的基本用法: cv::HoughLines(image, lines, rho, theta, threshold);image: 输入的二值图像,通常是通过边缘检测算法生成的。…...
el-table 边框颜色修改 简单有效!
废话不多说,直接上图 (1)修改前的图如下: 以上是elementUI原组件自带的样式 (2)下面是修改后的边框图如下: 源码如下: <el-table :data"jctableData" border size…...
Zabbix第二部分:基于Proxy分布式部署实现Web监控和Zabbix HA集群的搭建
代理和高可用 一、基于zabbix-proxy的分布式监控1.1 分布式监控的作用1.2 数据流向1.3 构成组件 二、部署zabbix代理服务器Step1 前置准备Step2 设置 zabbix 的下载源,安装 zabbix-proxyStep3 部署数据库并将zabbix相关文件导入Step4 修改zabbix-proxy的配置文件&am…...
JumpServer rce深入剖析
影响范围 JumpServer < v2.6.2 JumpServer < v2.5.4 JumpServer < v2.4.5 JumpServer v1.5.9 修复链接及参考 修改了一处代码: Git History 增加了一处鉴权 def connect(self):user self.scope["user"]if user.is_authenticated and …...
EasyExcel导入/导出Excel文件
EasyExcel导入/导出Excel文件简单写法 1、导入依赖 2、创建简单导入、导出demo 3、创建类 继承AnalysisEventListener(导入Excel监听解析表格数据) 4、创建类 基于注解 自定义Excel导出模版 1、导入EasyExcel依赖 <!--导入EasyExcel…...
力扣(LeetCode)2512. 奖励最顶尖的K名学生(C++)
优先队列哈希集合反向思维(或自定义排序) 模拟,请直接看算法思路: 两个哈希集合S1和S2, S1存正面词汇,S2存负面词汇;一个优先队列pq,pq存{score, id}键值对,即学生分数-学生id。 算法流程: 初…...
CubeMX+BabyOS 使用方法
MCU:STM32G030F 编译器:MDK 托管工具:Sourcetree CubeMX创建工程 BabyOS克隆 添加子模块 git submodule add https://gitee.com/notrynohigh/BabyOS.git BabyOS 切换dev 分支 查看当前分支 git branch -a 切换本地分支到dev git che…...
OpenResty安装-(基于Nginx的高性能Web平台,可在Nginx端编码业务)
文章目录 安装OpenResty1.安装1)安装开发库2)安装OpenResty仓库3)安装OpenResty4)安装opm工具5)目录结构6)配置nginx的环境变量 2.启动和运行3.备注 安装OpenResty 1.安装 首先你的Linux虚拟机必须联网 …...
算法-DFS+记忆化/动态规划-不同路径 II
算法-DFS记忆化/动态规划-不同路径 II 1 题目概述 1.1 题目出处 https://leetcode.cn/problems/unique-paths-ii 1.2 题目描述 2 DFS记忆化 2.1 思路 注意题意,每次要么往右,要么往下走,也就是说不能走回头路。但是仍有可能走到之前已经…...
黑盒测试方法:原理+实战
目录 一、如何设计测试用例 二、黑盒测试常用方法 1、基于需求进行测试用例的设计 2、等价类 3、边界值 4、判定表分析法(因果分析法) 5、正交表 6、场景设计法 三、案例补充 1、使用Fiddler模拟弱网 2、针对一个接口该如何测试 一、如何设计测试…...
【网络】每天掌握一个Linux命令 - iftop
在Linux系统中,iftop是网络管理的得力助手,能实时监控网络流量、连接情况等,帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...
SciencePlots——绘制论文中的图片
文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了:一行…...
在rocky linux 9.5上在线安装 docker
前面是指南,后面是日志 sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo dnf install docker-ce docker-ce-cli containerd.io -y docker version sudo systemctl start docker sudo systemctl status docker …...
什么是EULA和DPA
文章目录 EULA(End User License Agreement)DPA(Data Protection Agreement)一、定义与背景二、核心内容三、法律效力与责任四、实际应用与意义 EULA(End User License Agreement) 定义: EULA即…...
C++ 求圆面积的程序(Program to find area of a circle)
给定半径r,求圆的面积。圆的面积应精确到小数点后5位。 例子: 输入:r 5 输出:78.53982 解释:由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982,因为我们只保留小数点后 5 位数字。 输…...
【7色560页】职场可视化逻辑图高级数据分析PPT模版
7种色调职场工作汇报PPT,橙蓝、黑红、红蓝、蓝橙灰、浅蓝、浅绿、深蓝七种色调模版 【7色560页】职场可视化逻辑图高级数据分析PPT模版:职场可视化逻辑图分析PPT模版https://pan.quark.cn/s/78aeabbd92d1...
使用LangGraph和LangSmith构建多智能体人工智能系统
现在,通过组合几个较小的子智能体来创建一个强大的人工智能智能体正成为一种趋势。但这也带来了一些挑战,比如减少幻觉、管理对话流程、在测试期间留意智能体的工作方式、允许人工介入以及评估其性能。你需要进行大量的反复试验。 在这篇博客〔原作者&a…...
mac 安装homebrew (nvm 及git)
mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用: 方法一:使用 Homebrew 安装 Git(推荐) 步骤如下:打开终端(Terminal.app) 1.安装 Homebrew…...
探索Selenium:自动化测试的神奇钥匙
目录 一、Selenium 是什么1.1 定义与概念1.2 发展历程1.3 功能概述 二、Selenium 工作原理剖析2.1 架构组成2.2 工作流程2.3 通信机制 三、Selenium 的优势3.1 跨浏览器与平台支持3.2 丰富的语言支持3.3 强大的社区支持 四、Selenium 的应用场景4.1 Web 应用自动化测试4.2 数据…...
给网站添加live2d看板娘
给网站添加live2d看板娘 参考文献: stevenjoezhang/live2d-widget: 把萌萌哒的看板娘抱回家 (ノ≧∇≦)ノ | Live2D widget for web platformEikanya/Live2d-model: Live2d model collectionzenghongtu/live2d-model-assets 前言 网站环境如下,文章也主…...
