Part 1.梳理思维导图

一.运算符重载

1.运算符重载的作用

使原本只能对基本数据类型生效的运算符,在重载后,满足可以对构造类型数据生效。

2.关系运算符重载

        a.关系运算符种类

>         >=         <         <=         ==         !=

        b.分析参数

表达式:左值 关系运算符 右值

左值:只能为左值

右值:可以为左值,也可以为右值

返回值:bool(ture or false)

        c.函数格式

成员函数:bool operator关系运算符(const 类名 &R) const

全局函数:bool operator关系运算符(const类名 &L,const 类名 &R)

        d.分析

因为返回值使bool类型,所以函数头为bool;

operator关系运算符为函数名,由于成员函数的实现方式是左值调用函数参数为右值,所以参数只有右值;

由于不改变右值,所以该成员函数后面加上const,使其成为常成员函数;

全局函数中实现方式为一个函数调用左值和右值就行关系运算,所以参数为两个,因为关系运算不改变参数值,所以加const

        e.例子

以关系运算符>为例,当需要全局函数实现时,需要在类内添加友元

#include <iostream>using namespace std;class num
{
//    friend const num operator+(const num &L,const num &R);
//    friend bool operator>(const num &L,const num &R);
private:int a;int b;
public:num(){}num(int a,int b):a(a),b(b){}const num operator+(const num &R) const{num temp;temp.a = a + R.a;temp.b = b + R.b;return temp;}//成员函数实现bool operator>(const num &R) const{if(a > R.a && b < R.b)return true;elsereturn false;}void show(){cout << "a = " << a << " b = " << b << endl;}
};//const num operator+(const num &L,const num &R)
//{
//    num temp;
//    temp.a = L.a + R.a;
//    temp.b = L.b + R.b;
//    return temp;
//}//全局函数实现,需要在类内添加友元
//bool operator>(const num &L,const num &R)
//{
//    if(L.a > R.a && L.b < R.b)
//        return true;
//    else
//        return false;
//}ostream & operator<<(ostream &cout,const num &n)
{cout << n.a << " " << n.b << endl;return cout;
}istream & operator>>(istream &cin,num &n)
{cin >> n.a >> n.b;return cin;
}int main()
{num n1(2,3);num n2(3,4);num n3 = n1+n2;n3.show();if(n1 > n2)cout << "n1 > n2" << endl;elsecout << "n1 < n2" << endl;return 0;
}

3.赋值运算符重载

        a.赋值运算符种类

+=        -=        *=        /=        %=

        b.分析参数

表达式:左值 赋值运算符 右值(a += b)

左值:只能为左值

右值:可以为左值,也可以为右值

返回值:自身的引用

        c.函数格式

成员函数:类名 &operator赋值运算符(const 类名 &R)

全局函数:类名 &operator赋值运算符(类名 &L,const 类名 &R)

        d.分析

因为成员函数和全局函数返回的均是左值的引用,所以函数的返回值类型为 类名 &;

成员函数实现方式时将右值作为参数给左值调用,所以只有右值为参数;

全局变量需要二者均为参数,但是由于右值会发生变化,所以只有左值加const;

        e.例子
#include <iostream>using namespace std;class num
{
//    friend const num operator+(const num &L,const num &R);
//    friend bool operator>(const num &L,const num &R);
//    friend num & operator+=(num &L,const num &R);private:int a;int b;
public:num(){}num(int a,int b):a(a),b(b){}const num operator+(const num &R) const{num temp;temp.a = a + R.a;temp.b = b + R.b;return temp;}bool operator>(const num &R) const{if(a > R.a && b < R.b)return true;elsereturn false;}//成员函数实现num &operator+=(const num &R){a = a + R.a;b = b + R.b;return *this;}void show(){cout << "a = " << a << " b = " << b << endl;}
};//const num operator+(const num &L,const num &R)
//{
//    num temp;
//    temp.a = L.a + R.a;
//    temp.b = L.b + R.b;
//    return temp;
//}//bool operator>(const num &L,const num &R)
//{
//    if(L.a > R.a && L.b < R.b)
//        return true;
//    else
//        return false;
//}//全局函数实现
//num & operator+=(num &L,const num &R)
//{
//    L.a = L.a + R.a;
//    L.b = L.b + R.b;
//    return  L;
//}int main()
{num n1(2,3);num n2(3,4);num n3 = n1+n2;n3.show();//    if(n1 > n2)
//        cout << "n1 > n2" << endl;
//    else
//        cout << "n1 < n2" << endl;n1 += n2 += n3;n1.show();n2.show();n3.show();return 0;
}

4.自增自减运算符

        a.自增自减运算符种类

++(前自增和后自增)        --(前自减和后自减)

        b.分析参数

表达式:前:自增自减运算符 值(++a)        后:值 自增自减运算符(a++)

值:只能为左值

返回值:前:自身的引用        后:右值

        c.函数格式

成员函数:前:类名 &operator自增自减运算符()

                  后:const 类名 operator自增自减运算符(int )

全局函数:前:类名 &operator自增自减运算符(类名 &O)

                  后:const 类名 operator自增自减运算符(类名 &O,int)

        d.前自增和后自增的区别

前自增返回段值是一个左值,可以继续被使用于函数,而后自增返回的是一个右值,不能被继续适用于函数

        e.分析

因为自增自减函数只需要调用自己,所以成员函数内没参数,全局函数内参数为自己;

前自增函数和后自增函数都是一样的引用,为了能满足函数重载,使函数使用的更方便,在后自增函数中增加一个哑元,使其满足函数重载的定义(参数个数不同或者参数类型不同);

前自增的返回值类型是类名 &(类的引用)是个左值,后自增返回的是类名(类的右值返回)是个右值;

        f.例子

因为后自增是返回没自增前的值,所以需要定义一个新的临时类成员,用于返回

#include <iostream>using namespace std;class num
{
//    friend const num operator+(const num &L,const num &R);
//    friend bool operator>(const num &L,const num &R);
//    friend num & operator+=(num &L,const num &R);
//    friend num &operator++(num &L);
//    friend const num operator++(num &L,int);
private:int a;int b;
public:num(){}num(int a,int b):a(a),b(b){}const num operator+(const num &R) const{num temp;temp.a = a + R.a;temp.b = b + R.b;return temp;}bool operator>(const num &R) const{if(a > R.a && b < R.b)return true;elsereturn false;}num &operator+=(const num &R){a = a + R.a;b = b + R.b;return *this;}//成员函数实现前自增num &operator++(){a++;b++;return *this;}//成员函数实现后自增const num operator++(int){num temp;temp.a = a++;temp.b = b++;return temp;}void show(){cout << "a = " << a << " b = " << b << endl;}
};//const num operator+(const num &L,const num &R)
//{
//    num temp;
//    temp.a = L.a + R.a;
//    temp.b = L.b + R.b;
//    return temp;
//}//bool operator>(const num &L,const num &R)
//{
//    if(L.a > R.a && L.b < R.b)
//        return true;
//    else
//        return false;
//}//num & operator+=(num &L,const num &R)
//{
//    L.a = L.a + R.a;
//    L.b = L.b + R.b;
//    return  L;
//}//全局函数实现前自增
//num &operator++(num &L)
//{
//    ++L.a;
//    ++L.b;
//    return L;
//}//全局函数实现后自增
//const num operator++(num &L,int)
//{
//    num temp;
//    temp.a = L.a++;
//    temp.b = L.b++;
//    return temp;
//}int main()
{num n1(2,3);num n2(3,4);num n3 = n1+n2;n3.show();//    if(n1 > n2)
//        cout << "n1 > n2" << endl;
//    else
//        cout << "n1 < n2" << endl;//    n1 += n2 += n3;
//    n1.show();
//    n2.show();
//    n3.show();cout << "++++++++++++++++++" << endl;n1.show();++n1;n1.show();cout << "++++++++++++++++++" << endl;n1.show();n2.show();n2 = n1++;n1.show();n2.show();return 0;
}

5.插入提取运算符重载

        a.插入提取运算符类型

<<插入运算符        >>提取运算符

        b.分析参数

表达式:左值 插入提取运算符 右值(cout << a)

左值:istream类的cin 或者 ostream类的cout

右值:类成员

返回值:iostram类的cin或者cout的引用

        c.函数格式

成员函数:因为返回值类型为iostram类,调用的函数为该类的函数,不适合在右值类内创建成员函数

全局函数:cout:ostream &operator<<(ostream &cout,const 类名 &a)

                  cin:istream &operator>>(istream &cin,类名 &a)

        d.例子
#include <iostream>using namespace std;class num
{
//    friend const num operator+(const num &L,const num &R);
//    friend bool operator>(const num &L,const num &R);
//    friend num & operator+=(num &L,const num &R);
//    friend num &operator++(num &L);
//    friend const num operator++(num &L,int);friend ostream & operator<<(ostream &cout,const num &n);friend istream & operator>>(istream &cin,num &n);
private:int a;int b;
public:num(){}num(int a,int b):a(a),b(b){}const num operator+(const num &R) const{num temp;temp.a = a + R.a;temp.b = b + R.b;return temp;}bool operator>(const num &R) const{if(a > R.a && b < R.b)return true;elsereturn false;}num &operator+=(const num &R){a = a + R.a;b = b + R.b;return *this;}num &operator++(){a++;b++;return *this;}const num operator++(int){num temp;temp.a = a++;temp.b = b++;return temp;}void show(){cout << "a = " << a << " b = " << b << endl;}
};//const num operator+(const num &L,const num &R)
//{
//    num temp;
//    temp.a = L.a + R.a;
//    temp.b = L.b + R.b;
//    return temp;
//}//bool operator>(const num &L,const num &R)
//{
//    if(L.a > R.a && L.b < R.b)
//        return true;
//    else
//        return false;
//}//num & operator+=(num &L,const num &R)
//{
//    L.a = L.a + R.a;
//    L.b = L.b + R.b;
//    return  L;
//}//num &operator++(num &L)
//{
//    ++L.a;
//    ++L.b;
//    return L;
//}//const num operator++(num &L,int)
//{
//    num temp;
//    temp.a = L.a++;
//    temp.b = L.b++;
//    return temp;
//}//全局函数实现<<
ostream & operator<<(ostream &cout,const num &n)
{cout << n.a << " " << n.b << endl;return cout;
}//全局函数实现>>
istream & operator>>(istream &cin,num &n)
{cin >> n.a >> n.b;return cin;
}int main()
{num n1(2,3);num n2(3,4);num n3 = n1+n2;n3.show();//    if(n1 > n2)
//        cout << "n1 > n2" << endl;
//    else
//        cout << "n1 < n2" << endl;//    n1 += n2 += n3;
//    n1.show();
//    n2.show();
//    n3.show();cout << "++++++++++++++++++" << endl;n1.show();++n1;n1.show();cout << "++++++++++++++++++" << endl;n1.show();n2.show();n2 = n1++;n1.show();n2.show();return 0;
}

二.静态成员

1.目的

申请静态成员是让每个类的这个静态成员实现共享

2.注意事项

静态成员不能在类内初始化,只能在类外初始化

静态成员函数只能访问静态成员

3.格式

class 类名
{int a;static 数据类型 变量名; // 静态数据成员static 返回值类型 函数名(形参列表)// 静态成员函数{ }};数据类型 类名::变量名 = 初始化;

4.例子

该函数为模拟银行存储,balance为余额,rate为利率,静态成员利率会在函数外改变,并使每一个账户成员利率一起改,静态函数static double getrate()只能调用静态成员rate,static double getmenory(Bankaccent &accent)静态函数为了调用非静态成员balance,只能调用类对象再调用

#include <iostream>using namespace std;class Bankaccent
{
private:double balance;static double rate;
public:Bankaccent(){}Bankaccent(double balance):balance(balance){};static double getrate(){return rate;}static void setrate(double setrate){rate = setrate;}static double getmenory(Bankaccent &accent){return accent.balance*(1+rate);}};double Bankaccent::rate = 0.05;int main()
{Bankaccent accent1(1000);cout << Bankaccent::getrate() << endl;Bankaccent::setrate(0.06);cout << Bankaccent::getrate() << endl;cout << Bankaccent::getmenory(accent1) << endl;return 0;
}

三.继承

1.目的

1.实现代码的复用性(重用性)

2.建立父类和子类之间的联系

3.通过继承 实现多态

2.继承的概念

在已有的一个类基础上,衍生出一个新的类,此为继承

被继承的类成为父类

继承的类成为子类

子类拥有父类所有成员和成员函数

3.格式

class 类名 : 继承方式 类名
{子类的拓展;
};

4.继承方式

1.public:父类的成员继承方式按原继承方式保存。原父类中,public,protected类可以访问private子类不能访问

2.protected:父类的成员继承方式按原继承方式为public改为protected保存,其他继承方式不变。原父类中,public,protected类可以访问private子类不能访问

3.private:父类的成员继承方式全按private继承方式保存。原父类中,public,protected类可以访问private子类不能访问

Part 2.程序

搭建一个货币的场景,创建一个名为 RMB 的类,该类具有整型私有成员变量 yuan(元)、jiao(角)和 fen(分),并且具有以下功能:

(1)重载算术运算符 + 和 -,使得可以对两个 RMB 对象进行加法和减法运算,并返回一个新的 RMB 对象作为结果。

(2)重载关系运算符 >,判断一个 RMB 对象是否大于另一个 RMB 对象,并返回 true 或 false。

(3)重载前置减减运算符 --,使得每次调用时 RMB 对象的 yuan、jiao 和 fen 分别减 1

(4)重载后置减减运算符 --,使得每次调用时 RMB 对象的 yuan、jiao 和 fen 分别减 1

(5)另外, RMB 类还包含一个静态整型成员变量 count,用于记录当前已创建的 RMB 对象的数量。每当创建一个新的 RMB 对象时,count 应该自增 1;每当销毁一个 RMB 对象时,count 应该自减 1。

要求,需要在main 函数中测试上述RMB 类的功能

#include <iostream>using namespace std;class RMB
{
private:int yuan;int jiao;int fen;static int count;
public:RMB(){countplus();}RMB(int yuan,int jiao,int fen):yuan(yuan),jiao(jiao),fen(fen){countplus();}RMB(const RMB& other):yuan(other.yuan),jiao(other.jiao),fen(other.fen){countplus();}~RMB(){countdecrease();}const RMB operator+(const RMB &R){RMB temp;temp.yuan = yuan+R.yuan;temp.jiao = jiao+R.jiao;temp.fen = fen+R.fen;return temp;}const RMB operator-(const RMB &R){RMB temp;temp.yuan = yuan-R.yuan;temp.jiao = jiao-R.jiao;temp.fen = fen-R.fen;return temp;}bool operator>(const RMB &R){if(yuan > R.yuan)return true;elsereturn false;}RMB &operator--(){--yuan;--jiao;--fen;return *this;}const RMB operator--(int){RMB temp;temp.yuan = yuan--;temp.jiao = jiao--;temp.fen = fen--;return temp;}static void countplus(){count++;}static void countdecrease(){count--;}static int countshow(){return count;}void show(){cout << yuan << " " << jiao << " " << fen << endl;}
};int RMB::count = 0;int main()
{{cout << RMB::countshow() << endl;RMB r1(100,99,88);cout << RMB::countshow() << endl;RMB r2(55,101,76);cout << RMB::countshow() << endl;cout << "算数运算符+" << endl;r1 = r1+r2;r1.show();cout << "算数运算符-" << endl;r1 = r1-r2;r1.show();cout << "关系运算符>" << endl;bool a = r1 > r2;cout << a << endl;cout << "前自减运算符--" << endl;--r1;r1.show();cout << "后自减运算符--" << endl;RMB r3 = r2--;r3.show();r2.show();}cout << RMB::countshow() << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/95321.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/95321.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/95321.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Cloudflare安全规则实用指南:从路径拦截到IP限制的10个经典范例

前言&#xff1a;在Cloudflare的安全防护体系中&#xff0c;自定义规则是抵御特定威胁的“精准武器”。除了基础的路径拦截&#xff0c;日常运维中还有许多高频场景需要针对性配置。本文将通过10个实用范例&#xff0c;带你掌握Cloudflare规则的灵活用法&#xff0c;覆盖路径防…

数据结构(时空复杂度)

目录 一、算法复杂度 二、时间复杂度 1.不同时间度代码举例 三、空间复杂度 一、算法复杂度 算法复杂度&#xff08;评估算法优劣一个重要指标&#xff09;分为时间复杂度和空间复杂度。 时间复杂度是指执行算法所需要的计算工作量&#xff0c;而空间复杂度是指执行这个…

ESXI8多网卡链路聚合

1. 背景 测试服务器只有千兆网卡&#xff0c;增加上行带宽&#xff0c;使用两块网卡做链路聚合。 2. 环境 VM ESXI 8.0 华为交换机 S5735S 3. 交换机配置 负载均衡方式采用了src-dst-ipTrunk模式采用了手工负载分担&#xff08;测试了静态LACP&#xff0c;未成功&#xff09;4.…

从“人工驱动”到“AI协同”:良策金宝AI如何助力设计院数智化跃迁?

在“双碳”目标驱动下&#xff0c;电力工程项目的数量与复杂度持续攀升。设计院面临“项目多、周期短、人力紧”的三重压力&#xff0c;传统的“人工驱动”模式已难以为继。良策金宝AI&#xff0c;正是这场变革的核心引擎。它以AI驱动数智化服务&#xff0c;为工程设计企业提供…

vue3 vite 自适应方案

两种方案&#xff1a; 1 使用 postcss-pxtorem插件 npm install postcss-pxtorem autoprefixer --save-dev # 或 yarn add postcss-pxtorem autoprefixer -D 2、postcss-px-to-viewport npm install postcss-px-to-viewport --save-dev 或 yarn add postcss-px-to-viewport -D …

华为研发投资与管理实践(IPD)读书笔记

在全球科技产业竞争日趋激烈的背景下&#xff0c;企业研发管理早已告别 “依赖个体经验、靠运气突破” 的粗放时代&#xff0c;如何将研发创新从 “偶然成功” 转化为 “可复制、可持续的必然成果”&#xff0c;成为所有追求长期竞争力的企业必须破解的命题。华为&#xff0c;作…

【LeetCode_283】移动零

刷爆LeetCode系列LeetCode第283题&#xff1a;github地址前言题目描述题目与思路分析代码实现算法代码优化LeetCode第283题&#xff1a; github地址 有梦想的电信狗 前言 本文用C实现 LeetCode 第283题 题目描述 题目链接&#xff1a;https://leetcode.cn/problems/move-z…

一文弄懂C/C++不定参数底层原理

目录 一、C语言的可变参数&#xff1a;基于栈帧的手动读取 &#xff08;1&#xff09;C函数调用的栈帧结构 &#xff08;2&#xff09;C 可变参数的 4 个核心宏&#xff1a;如何 “手动读栈” &#xff08;3&#xff09;实战代码&#xff1a;用 C 可变参数实现求和函数 &a…

【Android】【设计模式】抽象工厂模式改造弹窗组件必知必会

写一个 Android 版本的抽象工厂弹窗 Manager 管理器&#xff0c;使用 DialogFragment 实现&#xff0c;这样能更贴近真实的开发场景。结构设计 抽象产品&#xff1a;BaseDialogFragment&#xff08;继承 DialogFragment&#xff09;具体产品&#xff1a;LoginDialogFragment, …

Win64OpenSSL-3_5_2.exe【安装步骤】

官网下载 注意&#xff1a;科学上网&#xff0c;可以加速下载速度&#xff01; Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions 下载后得到&#xff1a;Win64OpenSSL-3_5_2.exe 双击安装 修改安装路径&#xff1a; 默认就选择第一个。 重要提醒​…

华为云云原生架构赋能:大腾智能加速业务创新步伐

巨大的涡轮、细小的螺丝&#xff0c;一台航天飞机发动机的三维模型呈现在屏幕上&#xff0c;远程同事同步协作&#xff0c;一台复杂设备在工程师高效的协同中不断完善。深圳市大腾信息技术有限公司&#xff0c;正是这场工业变革的推动者之一。大腾智能以“云原生工业”的融合为…

基于https+域名的Frp内网穿透教程(Linux+Nginx反向代理)

系列文章目录 基于http公网ip的Frp内网穿透教程(win server) 基于http域名的Frp内网穿透教程(win serverIIS反向代理) 基于http公网ip的Frp内网穿透教程(Linux) 基于https域名的Frp内网穿透教程(LinuxNginx反向代理) 目录 系列文章目录 前言 一、Frp是什么&#xff1f; 1. …

裸机程序(1)

一、裸机裸机是一个在计算机硬件与软件开发领域高频出现的概念&#xff0c;核心定义是 “未安装操作系统&#xff08;OS&#xff09;&#xff0c;仅包含硬件本身&#xff08;或仅运行最底层硬件驱动 / 控制程序&#xff09;的设备”。在电脑中&#xff0c;裸机会映射代码到cpu&…

95%企业AI失败?揭秘LangGraph+OceanBase融合数据层如何破局!​

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。不知道你们有没有遇到过&#xff0c;在我们一些实际落地的AI项目中&#xff0c;虽然前期“Demo 很惊艳&#xff0c;但上线后却无人问津”。你们有没有想…

树莓集团产教融合:数字学院践行职业教育“实体化运营”要求

在职业教育改革不断深化的背景下&#xff0c;“实体化运营” 成为推动职业教育高质量发展的重要方向。树莓集团积极响应这一要求&#xff0c;以产教融合为核心&#xff0c;打造数字学院&#xff0c;切实践行职业教育 “实体化运营”&#xff0c;为培养高素质数字领域专业人才探…

ELK 统一日志分析系统部署与实践指南(上)

#作者&#xff1a;张桐瑞 文章目录1 ELK 技术栈概述1.1ELK 核心组件详解1.2 ELK 工作流程2 ELK部署2.1 环境描述2.1.7 配置es集群下篇&#xff1a;《ELK 统一日志分析系统部署与实践指南&#xff08;下&#xff09;》 链接: [https://blog.csdn.net/qq_40477248/article/detail…

上位机知识篇---poweshellcmd

要理解 PowerShell 和 CMD 的区别&#xff0c;我们可以先打个通俗的比方&#xff1a;CMD 像老式功能机&#xff0c;只能干打电话、发短信这些 “基础活”&#xff1b;而 PowerShell 像智能手机&#xff0c;不仅能做基础操作&#xff0c;还能装 APP、玩复杂功能&#xff0c;甚至…

利用 Python 绘制环形热力图

暑假伊始&#xff0c;Coldrain 参加了学校举办的数模集训&#xff0c;集训的过程中&#xff0c;遇到了需要展示 59 个特征与 15 个指标之间的相关性的情况&#xff0c;在常用的图表不大合适的情况下&#xff0c;学到了一些厉害的图表&#xff0c;但是似乎千篇一律都是用 R 语言…

【序列晋升】27 Spring Cloud Sleuth给分布式系统装上透视镜

Spring Cloud Sleuth作为微服务架构中的核心监控组件&#xff0c;通过轻量级的无侵入式跟踪机制&#xff0c;解决了分布式系统中请求路径复杂、问题定位困难的痛点。它自动为每个服务请求创建唯一的Trace ID&#xff0c;并为每个服务间调用生成Span ID&#xff0c;形成完整的调…

Linux(2)|入门的开始:Linux基本指令(2)

一、基本指令介绍 回顾上篇博客Linux(1)|入门的开始&#xff1a;Linux基本指令-CSDN博客&#xff0c;我们已经学习了mkdir目录的创建&#xff0c;touch普通文件的创建&#xff0c;光有创建肯定是不行的&#xff0c;接下来就介绍我们的删除指令 1、rmdir指令&&rm指令 …