加号运算符重载

#include<iostream>
using namespace std;//对于内置数据类型,编译器知道如何运算
//加号运算符重载
class Person {
public:int m_a;int m_b;//成员函数实现加号运算符重载//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;//}};
//全局函数实现加号运算符重载
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;
}
//函数重载
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;Person p2;p1.m_a = 11;p1.m_b = 12;p2.m_a = 13;p2.m_b = 14;Person p3 = p1 + p2;cout << "p3.m_a = " << p3.m_a << endl;cout << "p3.m_b = " << p3.m_b << endl;p3 = p3 + 100;cout << "p3.m_a = " << p3.m_a << endl;cout << "p3.m_b = " << p3.m_b << endl;}int main() {test01();return 0;
}

左移运算符重载

#include<iostream>
using namespace std;
//左移运算符重载
class Person {friend ostream& operator<< (ostream& cout, Person& p);//友元
public:Person() {m_a = 0;m_b = 0;}
private:int m_a;int m_b;//利用成员函数重载左移函数, 通常不会利用成员函数重载左移运算符/*void operator<<(Person& p) {}*/};ostream & operator<<(ostream &cout, Person &p) {cout << "m_a = " << p.m_a << " m_b = " << p.m_b;return cout;
}void test01() {Person p;cout << p << endl;
}int main() {test01();return 0;
}

递增运算符重载

#include<iostream>
using namespace std;
class MyInteger {friend ostream& operator<<(ostream& out, MyInteger myint);
public:MyInteger() {m_num = 0;}//重载前置++运算符 返回&是为了一直对一个数据进行操作MyInteger& operator++() {m_num++;//将自身作为返回return *this;}//重置后置++运算符 int代表占位参数 用于区分前置和后置递增//不反回& 因为局部变量temp会被释放MyInteger operator++(int) {//先记录当时结果MyInteger temp = *this;//后递增m_num++;//最后将记录的结果进行返回,因为这是后置++return temp;}private:int m_num;
};
//重载左移运算符
ostream& operator<<(ostream& out, MyInteger myint) {out << myint.m_num;return out;
}void test01() {MyInteger myint;cout << ++myint;}void test02() {MyInteger myint;cout << (myint++)++ << endl;;cout << myint;
}int main() {//test01();test02();return 0;
}

赋值运算符重载

#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 << "p2的年龄:" << *p3.m_age << endl;
}
int main() {test01();return 0;
}

关系运算符重载

#include<iostream>
#include<cstring>
using namespace std;
class Person {
public:Person(string name, int age) {m_name = name;m_age = age;}//重载关系运算符int operator==(Person& p) {if (this->m_age == p.m_age && this->m_name == p.m_name)return 1;elsereturn 0;}bool operator!=(Person& p) {if (this->m_age == p.m_age && this->m_name == p.m_name)return false;return true;}string m_name;int m_age;
};
void test01() {Person p1("tom", 18);Person p2("tom", 17);if (p1 == p2)cout << "p1和p2是相等的" << endl;elsecout << "p1和p2是不相等的" << endl;}void test02() {Person p1("tom", 18);Person p2("tom", 18);if (p1 != p2)cout << "p1和p2是不相等的" << endl;elsecout << "p1和p2是相等的" << endl;}int main() {test01();test02();return 0;
}

函数调用运算符重载

#include<iostream>
#include<cstring>
using namespace std;
//函数调用运算符重载
//打印输出类
class Myprint {
public://重载函数调用运算符void operator() (string test) {cout << test << endl;}};void test01() {Myprint myprint;//由于使用起来非常像函数调用,所以也叫仿函数myprint("hello motherfucker");}
//仿函数非常灵活,没有固定的写法
class Add {
public:int operator()(int num1, int num2) {return num1 + num2;}};void test02() {Add myadd;int res = myadd(100, 111);cout << "sum = " << res << endl;//匿名函数对象cout << Add()(100, 100) << endl;}int main() {//test01();test02();return 0;
}

C++(五)——运算符重载相关推荐

  1. C++实验五 运算符重载

    为实验二中编写的Songer(歌手)类重载"+".">"."==".前置"++".后置"++" ...

  2. 编写一个程序,用户使用for循环输入5个数字,所有这些数字将存储在一个数组中。之后,程序将添加这五个数字并显示结果。程序必须支持运算符重载的概念。

    Write a program in which users enter 5 numbers using for loop and all these numbers will store in an ...

  3. 对象的比较与排序(五):C#运算符重载

    c#里的运算符重载是个不常出现的应用,同时也是个好玩的东西. 我们可以使用他来实现这样的需求: 当一个订单与另外一个订单相加形成一个新的订单.新订单的货物为两个订单的货物的并集,总价为两个订单的总价和 ...

  4. 运算符重载、STL模板库、类型转换、异常处理机制(C++五)

    目录 一.运算符重载 1. 引入运算符重载 2.语法格式 3.运算符重载有两种表现形式 4.运算符重载分类 5.运算符重载的特点: 二.C++中的STL模板库的使用 1.容器类(container) ...

  5. c++重载(以运算符重载为主)

    重载(OverLoading)是面向对象程序设计多态性的一种体现.所谓重载,是指"同一标识符"在同一作用域的不同场合具有不同的语义,这个标识符可以是函数名或运算符.也就是说,重载可 ...

  6. C++运算符重载(成员函数方式)

    一.运算符重载 C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型,也需要有类似的运算操作.如果将C++中这些现存的运算符直接作用于用户自定义的类型数据上,会得到什么样 ...

  7. c++运算符重载中很多人易忘的小细节

    运算符重载的基本格式 运算符重载的本质就是函数重载 类型说明符 operator 运算符 (参数列表) { 函数体://实现运算符功能的代码. } 如果把 operator运算符"看作一个整 ...

  8. C++运算符重载讲解与经典实例

    转自:http://blog.csdn.net/dingyuanpu/article/details/5852825 C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型 ...

  9. 运算符重载(加减运算符、前置加加(减减)后置加加(减减)运算符、赋值运算符、输入输出运算符、关系运算符、函数调用)

    编译器对于一个类会默认生成以几种函数: 1.默认构造函数(空形参,空函数体) 2.默认拷贝构造函数(浅拷贝,也叫值拷贝.字节拷贝) 3.析构函数(空形参,空函数体.析构函数要求形参列表必须是空的,所以 ...

最新文章

  1. 【ACM】删数问题(待更)
  2. python文件的后缀名-python文件后缀是什么
  3. 安卓学习第17课——Gallery
  4. java数据类型的站位_Java 数据类型在实际开发中应用
  5. Image Style Transfer:多风格 TensorFlow 实现
  6. Pytorch模型构造方法
  7. Vb.net 获取IP地址
  8. shell编程三大神器之awk
  9. MemoryCache缓存help类
  10. 技术党求生骚操作!手把手教你做一只口红色号识别器!
  11. button 点击的涟漪效果
  12. [github] github入手教程
  13. 世界500强公司要求员工必须熟练掌握的七种工作方法
  14. 基于STM32的智能家居控制系统设计与实现(带红外遥控控制空调)
  15. 【考前冲刺整理】20220812
  16. 百度变更使命后首个政府AI合作落地:在李彦宏老家
  17. WPS为什么设置段前一行没反应
  18. android 进度条边框,android用户界面-组件Widget-进度条ProgressBar
  19. 百度云无法在网页上调用客户端进行下载文件(已安装最新版)
  20. APP上架应用宝外显流程

热门文章

  1. 【报错】No match for argument: mysql-community-server Error: Unable to find a match: mysql-community-s
  2. Vue怎么将后台(springboot)中的图片显示到前端表格中
  3. 鸿蒙发布的意义,华为鸿蒙正式发布!“鸿蒙”是什么意思?
  4. 2246xt u盘开卡详细教程_U盘制作PE及系统安装详细教程!
  5. 成功解决eric6 Create Dialog Code XXX.py exists but does not contain any classes
  6. 成功解决SQL Server软件中出现的18470问题
  7. Xshell:Xshell的简介、安装、使用方法之详细攻略
  8. NLP:基于nltk和jieba库对文本实现提取文本摘要(两种方法实现:top_n_summary和mean_scored_summary)
  9. 成功解决sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is amb
  10. AI之Robot:带你玩转机器人DIY机器人——让你成为机器人的真正主人