C++中运算符重载的示例分析
发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,这篇文章主要为大家展示了"C++中运算符重载的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"C++中运算符重载的示例分析"这篇文章吧。运算符重载运
千家信息网最后更新 2025年11月10日C++中运算符重载的示例分析
这篇文章主要为大家展示了"C++中运算符重载的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"C++中运算符重载的示例分析"这篇文章吧。
运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
加号运算符重载
作用:实现两个自定义数据类型相加的运算
#includeusing 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)
#includeusing 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 << 对象)
#includeusing 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 << coutostream& 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;}
递增运算符重载
通过重载递增运算符,实现自己的整型数据
#includeusing 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 << coutostream& 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;}
递减运算符重载
通过重载递减运算符,实现自己的整型数据
#includeusing 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 << coutostream& 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)
#includeusing 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;}
关系运算符重载
重载关系运算符,可以让两个自定义数据类型对象进行对比操作
#includeusing 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;}
函数调用运算符重载
特点:函数调用运算符 () 也可以重载;重载后调用的方式很像函数,被称为仿函数;没有固定写法,非常灵活
#includeusing namespace std;#include 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++
拷贝
类型
输出
示例
算符
中运
分析
内容
对象
篇文章
两个
作用
内存
参数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
河北什么是网络技术分类推广
传统村落保护网络技术
网络安全技术怎么做兼职
网络安全与道德资料
网络安全产品展
武汉晟莱悦武汉晟莱悦软件开发
查阅医学文献的数据库
全球科技互联网公司排名
服务器硬盘分区合并保留数据
神舟数码网络安全证书查询
附属医院网络安全管理制度
猛鬼宿舍修改服务器版
网络技术学院毕业证
网页怎么连入数据库sql
奉贤区智能化网络技术服务比较
移动网络安全设备管理规定
无限法则亚服服务器在哪里放着
网络安全硕士毕业生就业去向
拔掉网线后还会有网络安全风险吗
有名的网络技术外包公司
互联网科技的了解
软件开发的框架代码
杨树数据库
cdn加速游戏服务器
大话2最新服务器
加固服务器产品
管理系统服务器支持热插播
给数据库加索引语句
上海苏越网络技术服务有限公司
mysql数据库产品表代码