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

【黑马程序员】C++运算符重载

文章目录

  • 运算符重载
    • 加号运算符重载
      • 成员函数实现运算符重载
      • 全局函数实现运算符重载
      • 全局函数实现函数重载
    • 左移运算符重载
    • 递增运算符重载
    • 赋值运算符重载
    • 关系运算符重载
    • 函数调用运算符重载

运算符重载

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

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

  • 不要滥用运算符重载

加号运算符重载

  • 可以计算自定义数据类型

成员函数实现运算符重载

  • 成员函数实现运算符重载的本质 p2.operator(p1)
#include <iostream>using namespace std;class Person{
public:Person(){}Person(int a, int b) {this->a=a;this->b=b;}// 成员函数实现运算符重载Person operator+(const Person& p) {Person temp;temp.a=this->a+p.a;temp.b=this->b+p.b;return temp;}
public:int a;int b;
};void test() {Person p1(10,10);Person p2(10,10);// 成员函数实现运算符重载的本质 p2.operator(p1)Person p3 = p1+p2;cout << "p3.a: " << p3.a << endl;cout << "p3.b: " << p3.b << endl;
}int main(){test();return 0;
}

全局函数实现运算符重载

  • 全局函数实现运算符重载的本质是operator+(p1,p2)
#include <iostream>using namespace std;class Person{
public:Person(){}Person(int a, int b) {this->a=a;this->b=b;}
public:int a;int b;
};// 全局函数实现运算符重载
Person operator+(Person p1, Person p2) {Person temp;temp.a=p1.a+p2.a;temp.b=p1.b+p2.b;return temp;
}
// 全局函数实现函数重载void test() {Person p1(10,10);Person p2(10,10);// 全局函数实现运算符重载的本质是operator+(p1,p2)Person p3 = p1+p2;cout << "p3.a: " << p3.a << endl;cout << "p3.b: " << p3.b << endl;
}int main(){test();return 0;
}

全局函数实现函数重载

  • 通过函数重载实现不同类型的加法运算
#include <iostream>using namespace std;class Person{
public:Person(){}Person(int a, int b) {this->a=a;this->b=b;}
public:int a;int b;
};// 全局函数实现函数重载
Person operator+(Person p1, int num) {Person temp;temp.a=p1.a+num;temp.b=p1.b+num;return temp;
}void test() {Person p1(10,10);int num = 100;// 通过函数重载实现了Person和int类型的加法运算Person p3 = p1+num;cout << "p3.a: " << p3.a << endl;cout << "p3.b: " << p3.b << endl;
}int main(){test();return 0;
}

左移运算符重载

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

  • 重载左移运算符配合友元可以实现输出自定义数据类型

#include <iostream>using namespace std;class Person{friend ostream& operator<<(ostream& cout, Person p);
public:Person(int a, int b){this->a = a;this->b = b;}// 成员函数重载左移运算符的本质是p<<cout// 在C++中一般不适用成员函数重载左移运算符,因为无法实现cout<<p的格式// operator<<(cout) {}
private:int a;int b;
};// ostream& 返回cout这样可以在链式使用时继续追加
// 本质:operator<<(cout, p),简化为cout<<p
ostream& operator<<(ostream& cout, Person& p) {cout << "a= " << p.a <<" b= " << p.b;return cout;
}void test(){Person p(1,1);cout << p << endl;
}int main(){test();return 0;
}

递增运算符重载

  • 通过重载自增运算符,实现自己的整形数据
#include <iostream>using namespace std;class MyInt{friend ostream& operator<<(ostream& cout, MyInt i);
public:// 重载前置++运算符,返回引用为了一直对一个数据进行递增操作MyInt& operator++(){num++;return *this;}// 重载后置++运算符// void operator++(int) int代表占位参数,可以用于区分前置和后置递增MyInt operator++(int){MyInt temp = *this;num++;return temp;}
private:int num;
};ostream& operator<<(ostream& cout, MyInt i){cout << i.num;return cout;
}void test(){MyInt myint;cout << ++(++myint) << endl;MyInt myint1;cout << (myint++)++ << endl;
}int main(){test();return 0;
}

赋值运算符重载

#include <iostream>using namespace std;class Person{
public:Person(int age) {this->age=new int(age);}Person& operator=(const Person& p) {// 编译器提供的是浅拷贝// age=p.age;// 先判断是否有属性在堆区,如果有先释放,然后在深拷贝if (age != NULL) {delete age;age=NULL;}// 深拷贝age=new int(*p.age);return *this;}~Person(){if (age == NULL) {return;}delete age;age=NULL;}int* age;
};void test(){Person p1(18);Person p2(20);Person p3(30);p3=p2=p1;cout << *p1.age << endl;cout << *p2.age << endl;cout << *p3.age << endl;
}int main(){test();return 0;
}

关系运算符重载

#include <iostream>using namespace std;class Person{friend ostream& operator<<(ostream& cout, Person& p);
public:Person(int age) {this->age=age;}bool operator==(const Person& p) {return age==p.age;}bool operator!=(const Person& p) {return age!=p.age;}bool operator>(const Person& p) {return age>p.age;}bool operator>=(const Person& p) {return age>=p.age;}bool operator<(const Person& p) {return age<p.age;}bool operator<=(const Person& p) {return age<=p.age;}
private:int age;
};ostream& operator<<(ostream& cout, Person& p){cout << p.age << endl;return cout;
}void test(){Person p1(18);Person p2(20);cout << (p1== p2) << endl;cout << (p1!= p2) << endl;cout << (p1>= p2) << endl;cout << (p1> p2) << endl;cout << (p1<= p2) << endl;cout << (p1< p2) << endl;
}int main(){test();return 0;
}

函数调用运算符重载

  • 由于重载后的使用方式很像函数调用,因此也称为仿函数

  • 仿函数没有固定写法,非常灵活

#include <iostream>using namespace std;class Person{
public:// 打印类仿函数void operator()(string text) {cout << text << endl;}
};class MyAdd{
public:// 加法类仿函数int operator()(int num1, int num2) {return num1+num2;}
};void test(){Person p;// 由于使用起来非常类似于函数调用,因此也称为仿函数p("123");
}void test1(){MyAdd myAdd;cout << myAdd(1,2) << endl;// 使用匿名函数调用cout <<MyAdd()(100,10) << endl;
}int main(){test();test1();return 0;
}
http://www.lryc.cn/news/299575.html

相关文章:

  • Java中的乐观锁和悲观锁
  • 从Unity到Three.js(计时器、Transform)
  • 红日靶场(初学)
  • 【PyTorch】改变张量(Tensor)形状操作
  • 《金融人工智能:用python实现ai量化交易》
  • 位运算+leetcode ( 2 )
  • 17 ABCD数码管显示与动态扫描原理
  • 【Zigbee课程设计系列文章】Zigbee开发环境搭建
  • [Linux开发工具]项目自动化构建工具-make/Makefile
  • PLC_博图系列☞参数实例
  • LLaMA 2 和 QianWen-14B
  • 浅谈Java常见设计模式及实例
  • 【RISC-V DSP设计】基于CEVA DSP架构的指令集分析(一)-总体介绍
  • Rust标量类型详解
  • 【双指针】【C++算法】1537. 最大得分
  • golang常用库之-操作数据库ORM:GORM 包介绍 | 一些 GORM 提示和注意事项
  • Stream流学习笔记
  • 单片机——FLASH(2)
  • 个体诊所门诊电子处方开单管理系统软件,配方模板病历模板设置一键导入操作教程
  • ELAdmin 配置定时任务
  • 【服务器部署】Docker环境的安装
  • leetcode刷题--贪心算法
  • 《Java 简易速速上手小册》第5章:Java 开发工具和框架(2024 最新版)
  • Python json解析
  • [FFmpeg学习]从视频中获取图片
  • Redis集中管理Session和系统初始化参数详解
  • [网鼎杯 2020 朱雀组]phpweb
  • 情人节html代码
  • 键盘重映射禁用 CtrlAltDel 键的利弊
  • 【网工】华为设备命令学习(综合实验一)