运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

1.加号运算符重载

作用:实现两个自定义数据类型相加的运算

#include <iostream>
#include <string>
using namespace std; //加号运算符重载
class Person{
public:
//1.成员函数重载+号
Person operator+(Person &p){
Person temp;
temp.m_a = this->m_a + p.m_a;
temp.m_b = this->m_b + p.m_b;
return temp;
}
int m_a;
int m_b;
}; //2.全局函数重载+号
Person operator+(Person &p1,Person &p2){
Person temp;
temp.m_a = p1.m_a + p2.m_a;
temp.m_b = p1.m_b + p2.m_b;
return temp;
} //3.函数重载的版本
Person operator+(Person &p1,int num){
Person temp;
temp.m_a = p1.m_a + num;
temp.m_b = p1.m_b + num;
return temp;
} void test01(){
Person p1;
p1.m_a = 10;
p1.m_b = 10;
Person p2;
p2.m_a = 10;
p2.m_b = 10; //成员函数重载的本质调用
//Person p3 = p1.operator+(p2); //全局函数重载的本质调用
//Person p3 = operator+(p1,p2);
Person p3 = p1 + p2; //运算符重载也可以发生函数重载
Person p4 = p3 + 100; cout << "p3.m_a = " << p3.m_a <<endl;
cout << "p3.m_b = " << p3.m_b <<endl;
cout << "p4.m_a = " << p4.m_a <<endl;
cout << "p4.m_b = " << p4.m_b <<endl;
} int main(){
test01();
return 0;
}

总结1:对于内置的数据类型的表达式的运算符是不可能改变的

总结2:不要滥用运算符重载

2.左移运算符重载

作用:可以自输出自定义数据类型

#include <iostream>
#include <string>
using namespace std; //左移运算符重载
class Person{
public:
//1.利用成员函数重载左移运算符 p.operator<<(cout) 简化版本p<<cout
//通常不会利用成员函数重载左移运算符,因为无法实现cout在左侧
int m_a;
int m_b;
}; //2.利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,Person &p){
cout << "m_a = " << p.m_a << " m_b = " << p.m_b << endl;
return cout;
//必须返回引用,因为只能存在一个,不能开辟新的空间
} void test01(){
Person p;
p.m_a = 10;
p.m_b = 10; cout << "p.m_a = " << p.m_a <<endl;
cout << "p.m_b = " << p.m_b <<endl;
cout << p << endl;
} int main(){
test01();
return 0;
}
  • 通常不会利用成员函数重载左移运算符,因为无法实现cout在左侧

    利用成员函数重载左移运算符 p.operator<<(cout) 简化版本p<<cout

  • 左移运算符重载必须返回ostream引用,因为只能存在一个,不能开辟新的空间

3. 递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

注意:前置返回的是变量的引用,后置返回的是常量。所以++++c合法,而c++++不合法

#include <iostream>
#include <string>
using namespace std; //重载递增运算符 //自定义整型
class MyInterger{
friend ostream & operator<<(ostream &cout, MyInterger myint);
public:
MyInterger(){
m_num = 0;
}
//1.重载前置++运算符
//返回引用是为了一直对一个数据进行递增操作
MyInterger & operator++(){
m_num = m_num+1;
return *this; //返回自身
}
//2.重载后置++运算符
//int代表一个占位参数,可以用于区分前置和后置递增
//后置递增一定不能返回引用,因为temp为局部变量,函数运行完后自动释放
MyInterger operator++(int){
//先 记录当时结果
MyInterger temp = *this;
//后 递增
m_num = m_num+1;
//最后 返回所记录结果
return temp;
}
private:
int m_num;
}; //重载左移运算符
ostream & operator<<(ostream &cout, MyInterger myint){
cout << myint.m_num;
return cout;
} void test01(){
MyInterger myint;
cout << ++myint << endl;
} void test02(){
MyInterger myint;
cout << myint++ << endl;
cout << myint << endl;
} int main(){
test02();
return 0;
}

总结:前置递增返回的是引用,后置递增返回的是值

4.赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性值进行拷贝
  4. 赋值运算符operator=,对属性值进行拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include<iostream>
using namespace std; class Person {
public:
Person(int age) {
m_age = new int(age);
}
~Person() {
if (m_age != NULL) {
delete m_age;
m_age = NULL;
}
} //重载赋值运算符
Person& operator=(Person &p) {
//编译器提供的浅拷贝
//m_age = p.m_age
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_age != NULL) {
delete m_age;
m_age = NULL;
}
m_age = new int(*p.m_age);
return *this;
}
int *m_age;
}; void test01() {
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout << "p1的年龄为:" << *p1.m_age << endl;
cout << "p2的年龄为:" << *p2.m_age << endl;
cout << "p3的年龄为:" << *p3.m_age << endl;
} int main() {
test01();
system("pause");
return 0;
}

5.关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#include<iostream>
#include<string>
using namespace std; class Person {
public:
Person(string name, int age) {
m_name = name;
m_age = age;
}
bool operator==(Person &p) {
if (this->m_name == p.m_name && this->m_age == p.m_age) {
return true;
}
return false;
}
bool operator==(Person &p) {
if (this->m_name == p.m_name && this->m_age == p.m_age) {
return false;
}
return true;
}
string m_name;
int m_age;
}; void test01() {
Person p1("xia",18);
Person p2("xia", 18); if (p1 == p2) {
cout << "p1 == p2" << endl;
}
else {
cout << "p1 != p2" << endl;
}
} int main() {
test01();
return 0;
}

6.函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
#include<iostream>
#include<string>
using namespace std; //函数调用运算符的重载 //打印输出类
class MyPrint {
public:
//重载函数调用运算符
void operator()(string test) {
cout << test << endl;
}
}; class MyAdd {
public:
int operator()(int num1, int num2) {
return num1 + num2;
}
}; void MyPrint02(string test) {
cout << test << endl;
} void test01() {
MyPrint myPrint;
myPrint("hello world"); //由于使用起来非常类似于函数调用,因此称为仿函数
MyPrint02("hello world");
} void test02() {
MyAdd myadd;
int ret = myadd(100, 100);
cout << "ret = " << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 100) << endl;
} int main() {
test01();
test02();
return 0;
}

C++学习(12)—— 运算符重载的更多相关文章

  1. C++学习之运算符重载的总结

    C++学习之运算符重载的总结              运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供了一种方法,即运算符重载函数 ...

  2. C++学习笔记-运算符重载

    运算符重载使得用户自定义的数据以一种更简洁的方式工作 运算符重载规则 重载运算符的限制 可以重载的运算符 + - * / % ^ & | ~ ! = < > += -= *= /= ...

  3. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  4. C++学习26 运算符重载的概念和语法

    所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Overloading)也是一个道 ...

  5. STL学习之运算符(<<)重载问题和仿函数的实现

    /*   运算符<<的重载一直报错,   友原函数中可以访问到类的私有成员*/#include<iostream>using namespace std; class MyIn ...

  6. 运算符重载+日期类Date

    Hello,一只爱学习的鱼 大学学习C++运算符重载的时候,老师出了一道"运算符重载+类"的综合练习题,让我们来一起看看吧! 题目: 设计一个日期类Date,包括年.月.日等私有成 ...

  7. 初步C++运算符重载学习笔记&lt;3&gt; 增量递减运算符重载

    初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数     增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...

  8. C++运算符重载学习总结

    在C ++中,我们可以使运算符适用于用户定义的类. 这意味着C ++能够为运算符提供数据类型的特殊含义,这种能力称为运算符重载. 例如,我们可以在像String这样的类中重载运算符'+',这样我们就可 ...

  9. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

随机推荐

  1. c# 自定义按钮,渐变颜色(含中心向四周渐变,单方向渐变)

    废话不多言,直接代码: public class RoundButton : Button { bool clickBool = false; //1.设置圆形 //2.设置渐变色 //3.设置too ...

  2. docker 挂载主机目录 -v 和 --mount区别

    使用-v  时,如果宿主机上没有这个文件,也会自动创建, 但是如果使用--mount时,宿主机中没有这个文件会报错找不到这个文件,并创建失败

  3. C# IE浏览器操作类

    using System; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropSe ...

  4. 记录ssis的两个异常解决办法

    1.Foreach文件枚举提示变量为空的解决办法:把foreach属性窗口中的DelayValidation设置为True就可以了 2.csv导入数据库提示:无法在 unicode 和非 unicod ...

  5. Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”

    一.说明 在使用python3+selenium写自动升级程序的时侯,碰到一个弹出对话框需要点击确认的场景.弹出的对话框如下图所示. 对于弹框各种资料都说通过switch_to.alert属性获取对话 ...

  6. [转帖]【mount】Linux根目录空间不足

    [mount]Linux根目录空间不足 2019.04.15 21:30:47字数 1094阅读 107 一.问题背景 一台数据库服务器,突然监控告警,报根目录空间不足(no space left o ...

  7. mysql中,手动提交事务

    1: 在mysql中,手动提交事务的案例:CREATE PROCEDURE tfer_funds       (from_account int, to_account int, tfer_amoun ...

  8. 《C++ 习题与解析》笔记

    目录 ####Chapter-1 C++语言概述(错题) ####Chapter-2 类和对象 ####Chapter-3 引用 ####Chapter-4 友元函数 #### Chapter-5 运 ...

  9. laravels 使用laravel-wechat 组件

    一. laravels (5.4)使用 laravel-wechat (4.13),出现无法登陆的情况,显示没有code 错误,解决办法 前提:已经在laravels.php 的 cleaners 中 ...

  10. -Shell 教程 Bash 脚本 基础语法 MD

    目录 目录 Shell 简介 Shell 脚本 Shell 环境 第一个shell脚本 Shell 变量 定义变量 使用变量 只读变量 删除变量 Shell 字符串 单引号 双引号 字符串基本操作 S ...