C++类与对象之运算符重载详解
更新时间:2021年10月18日 12:00 点击:1692
运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
加号运算符重载
作用:实现两个自定义数据类型相加的运算
#include <iostream> using namespace std; class Person { public: // 构造函数 Person(int num1, int num2){ this->num1 = num1; this->num2 = num2; } // 加法函数 Person operator+(const Person &p){ Person temp(0, 0); temp.num1 = this->num1 + p.num1; temp.num2 = this->num2 + p.num2; return temp; } // 打印输出 void printMessage() { cout << "num1 = " << num1 << " num2 = " << num2 << endl; } private: int num1; int num2; }; void func() { Person p1(1, 2); Person p2(3, 4); Person p3 = p1.operator+(p2); // 可以简化为 Person = p1 + p2; p3.printMessage(); // num1 = 4 num2 = 6 Person p4 = p3 + p2; p4.printMessage(); // num1 = 7 num2 = 10 } int main() { func(); system("pause"); return 0; }
左移运算符重载
作用:输出自定义数据类型
第一种:成员函数(对象 << cout)
#include <iostream> using namespace std; class Person { public: // 构造函数 Person(int num1, int num2){ this->num1 = num1; this->num2 = num2; } // 左移运算符 p.operator<<(cout); 简化版本 p << cout void operator <<(ostream &cout) { cout << "num1 = " << num1 << " num2 = " << num2 << endl; } private: int num1; int num2; }; void func() { Person p1(1, 2); p1.operator<<(cout); // 简化为 p1 << cout Person p2(3, 4); p2 << cout; } int main() { func(); // outputs:num1 = 1 num2 = 2 // num1 = 3 num2 = 4 system("pause"); return 0; }
第二种:全局函数(cout << 对象)
#include <iostream> using namespace std; class Person { // 友元 friend ostream& operator <<(ostream &cout, Person &p); public: // 构造函数 Person(int num1, int num2){ this->num1 = num1; this->num2 = num2; } private: int num1; int num2; }; // 左移运算符 p.operator<<(cout); 简化版本 p << cout ostream& operator <<(ostream &cout, Person &p) { cout << "num1 = " << p.num1 << " num2 = " << p.num2; return cout; } void func() { Person p1(1, 2); operator<<(cout, p1); // 简化为 cout << p1; cout << endl; cout << p1 << " 你是猪!" << endl; } int main() { func(); // outputs:num1 = 1 num2 = 2 // num1 = 1 num2 = 2 你是猪! system("pause"); return 0; }
递增运算符重载
通过重载递增运算符,实现自己的整型数据
#include <iostream> using namespace std; class MyInteger { //friend ostream& operator <<(ostream &cout, MyInteger &p); friend ostream& operator <<(ostream &cout, MyInteger p); public: // 构造函数 MyInteger(){ this->m_num = 0; } // 重载前置递增运算符 MyInteger& operator++() { ++this->m_num; return *this; } // 重载后置递增运算符 MyInteger operator++(int) { // int 是占位参数,用于区分前置和后置递增 MyInteger temp = *this; ++this->m_num; return temp; } private: int m_num; }; // 用于前置递增,左移运算符 p.operator<<(cout); 简化版本 p << cout //ostream& operator <<(ostream &cout, MyInteger &p) { // cout << "m_num = " << p.m_num; // return cout; //} // 用于后置递增,左移运算符 p.operator<<(cout); 简化版本 p << cout ostream& operator <<(ostream &cout, MyInteger p) { cout << "m_num = " << p.m_num; return cout; } void func() { MyInteger m_int; //cout << m_int.operator++() << endl; // m_num = 1 //cout << ++m_int << endl; // m_num = 2 cout << m_int++ << endl; // m_num = 0 cout << m_int << endl; // m_num = 1 } int main() { func(); system("pause"); return 0; }
递减运算符重载
通过重载递减运算符,实现自己的整型数据
#include <iostream> using namespace std; class MyInteger { friend ostream& operator <<(ostream &cout, MyInteger &p); //friend ostream& operator <<(ostream &cout, MyInteger p); public: // 构造函数 MyInteger(){ this->m_num = 0; } // 重载前置递减运算符 MyInteger& operator--() { --this->m_num; return *this; } // 重载后置递减运算符 MyInteger operator--(int) { // int 是占位参数,用于区分前置和后置递增 MyInteger temp = *this; --this->m_num; return temp; } private: int m_num; }; // 用于前置递减,左移运算符 p.operator<<(cout); 简化版本 p << cout ostream& operator <<(ostream &cout, MyInteger &p) { cout << "m_num = " << p.m_num; return cout; } // 用于后置递减,左移运算符 p.operator<<(cout); 简化版本 p << cout //ostream& operator <<(ostream &cout, MyInteger p) { // cout << "m_num = " << p.m_num; // return cout; //} void func() { MyInteger m_int; cout << m_int.operator--() << endl; // m_num = -1 cout << --m_int << endl; // m_num = -2 //cout << m_int-- << endl; // m_num = 0 //cout << m_int << endl; // m_num = -1 } int main() { func(); system("pause"); return 0; }
赋值运算符重载
C++ 编译器至少给一个类添加 4 个函数(此处不举例,具体可见核心篇 5)
#include <iostream> using namespace std; class Student { public: // 构造函数 Student(int id) { m_id = new int(id); // 从堆区开辟内存用来存储 id } // 拷贝构造函数 Student(const Student &s) { this->m_id = new int(*s.m_id); } // 析构函数 ~Student() { if (m_id != NULL) { delete m_id; m_id = NULL; } } // 重载赋值运算符 Student& operator=(Student &s) { if (m_id != NULL) { delete m_id; m_id = NULL; } m_id = new int(*s.m_id); return *this; } // 取出 id int getId() { return *m_id; } private: int *m_id; }; void func() { Student s1(1); cout << s1.getId() << endl; // 1 // 用拷贝构造函数来作深拷贝 Student s2(s1); cout << s2.getId() << endl; // 1 // 用赋值重载运算符来作赋值 Student s3(2); // id 为 2 s1 = s3; // 复杂版本:s1.operator=(s3) cout << s1.getId() << endl; // 2 // 多段赋值运算符,例如 a = b = c Student s4(3); s1 = s2 = s3 = s4; // id 均为 3 cout << s1.getId() << s2.getId() << s3.getId() << s4.getId() << endl; // 3333 } int main() { func(); system("pause"); return 0; }
关系运算符重载
重载关系运算符,可以让两个自定义数据类型对象进行对比操作
#include <iostream> using namespace std; class Student { public: // 构造函数 Student(int id) { m_id = new int(id); // 从堆区开辟内存用来存储 id } // 拷贝构造函数 Student(const Student &s) { this->m_id = new int(*s.m_id); } // 析构函数 ~Student() { if (m_id != NULL) { delete m_id; m_id = NULL; } } // 重载 == 运算符 bool operator==(Student &s) { if (this->getId() == s.getId()) { return true; } else { return false; } } // 取出 id int getId() { return *m_id; } private: int *m_id; }; void func() { Student s1(1); Student s2(1); Student s3(2); if (s1 == s2) { cout << "s1 和 s2 相等" << endl; } if (s1 == s3) { cout << "s1 和 s3 相等" << endl; } else { cout << "s1 和 s3 不相等" << endl; } } int main() { func(); // outputs:s1 和 s2 相等 // s1 和 s3 不相等 system("pause"); return 0; }
函数调用运算符重载
特点:函数调用运算符 () 也可以重载;重载后调用的方式很像函数,被称为仿函数;没有固定写法,非常灵活
#include <iostream> using namespace std; #include <string> class Print { public: void operator()(string text) { cout << "利用函数调用重载运算符打印输出:" << text << endl; } }; void func() { Print p1; p1("你是一个小猪猪!"); // 利用函数调用重载运算符打印输出:你是一个小猪猪! } int main() { func(); // outputs:s1 和 s2 相等 // s1 和 s3 不相等 system("pause"); return 0; }
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更多内容!
上一篇: 聊一聊C++虚函数表的问题
下一篇: C++虚函数表深入研究
相关文章
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 本篇文章是对C#中方法重载进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章介绍了c#的异或运算符,有需要的朋友可以参考一下...2020-06-25
- 轻松学习C#的运算符,对C#的运算符感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的运算符。...2020-06-25
- 这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
- 虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
- 这只我自己在学C#中的一些总结,其中对于各级的划分方式、各操作符的优先级的理解并不见得正确,只是自己的看法,拿出来与大家分享...2020-06-25
- 这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25