欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > C++系列-运算符+重载

C++系列-运算符+重载

2024/10/25 2:31:49 来源:https://blog.csdn.net/weixin_48668114/article/details/132841539  浏览:    关键词:C++系列-运算符+重载

运算符+重载

  • 运算符重载含义
  • 重载的方式
    • 成员函数实现重载
    • 全局函数实现重载
  • 运算符重载也可以实现函数重载
  • 运算符实现连续运算

运算符重载含义

  • C++ 允许在同一作用域中的某个运算符指定多个定义,称为运算符重载。
  • 通过运算符重载,可以使得已有的运算符具有其它的功能,适应于不同的操作数类型,经常运算符重载应用于自定义数据类型。

重载的方式

  • 当成员函数和普通函数均可实现重载时,优先选用成员函数重载。
  • 内置数据类型的表达式的运算符不能重载。比如3+2, 这个+不能变化其含义。
  • 不要乱用重载,需要做到见名知意。

成员函数实现重载

code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";// 用普通的成员函数实现自定义类型相加Horse horse_plus(Horse& h1)		// 定义成员函数,实现Horse类型相加,返回Horse类型{Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}// 通过成员函数重载+,只是系统给自定义了名称operator+,使用+运算符和调用operator+及horse_plus函数是一样的Horse operator+ (Horse& h1){Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}};int main(){Horse h1;Horse h2;h1.age = 10;h2.age = 20;h1.color = "brown";h2.color = "white";cout << "horse_plus, age:" << h1.horse_plus(h2).age << endl;		// 使用了成员函数实现加法cout << "horse_plus, color: " << h1.horse_plus(h2).color << endl;	// 使用了成员函数实现加法cout << "+, age: " << (h1 + h2).age << endl;		// + 运算符重载cout << "+, color: " << (h1 + h2).color << endl;	// + 运算符重载cout << "operator+, age: " << h1.operator+(h2).age << endl;		// + 运算符重载调用的本质其实是成员函数调用cout << "operator+, color : " << h1.operator+(h2).color << endl;// + 运算符重载调用的本质其实是成员函数调用system("pause");return 0;}
result:horse_plus, age:30horse_plus, color: brownwhite+, age: 30+, color: brownwhiteoperator+, age: 30operator+, color : brownwhite

全局函数实现重载

code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";};// 通过全局函数重载+,系统给自定义了名称operator+,使用+运算符和调用operator+是一样的Horse operator+ (Horse& h1, Horse& h2)		// 全局函数重载,参数要传两个{Horse result;result.age = h1.age + h2.age;result.color = h1.color + h2.color;return result;}int main(){Horse h1;Horse h2;h1.age = 10;h2.age = 20;h1.color = "brown";h2.color = "white";// h1 + h2相当于是operator+(h1, h2)cout << "+, age: " << (h1 + h2).age << endl;			cout << "+, color: " << (h1 + h2).color << endl;cout << "operator+, age: " << operator+(h1, h2).age << endl;cout << "operator+, color : " << operator+(h1, h2).color << endl;system("pause");return 0;}
result:+, age: 30+, color: brownwhiteoperator+, age: 30operator+, color : brownwhite

运算符重载也可以实现函数重载

code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";Horse horse_plus(Horse& h1){Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}// 通过成员函数重载+,只是系统给自定义了名称operator+,和调用operator+及horse_plus函数是一样的。Horse operator+ (Horse& h1){Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}// 运算符重载也可以实现函数重载Horse operator+ (int num){Horse result;result.age = this->age + num;return result;}};int main(){Horse h1;Horse h2;h1.age = 10;h2.age = 20;h1.color = "brown";h2.color = "white";cout << "horse_plus, age:" << h1.horse_plus(h2).age << endl;cout << "horse_plus, color: " << h1.horse_plus(h2).color << endl;cout << "+, age: " << (h1 + h2).age << endl;					// 运算符重载实现的函数重载cout << "+, color: " << (h1 + h2).color << endl;				// 运算符重载实现的函数重载cout << "operator+, age: " << h1.operator+(h2).age << endl;		// 运算符重载调用的本质是函数调用cout << "operator+, color : " << h1.operator+(h2).color << endl;// 运算符重载调用的本质是函数调用cout << "operator+2, age: " << (h1 + 100).age << endl;			// 运算符重载实现的函数重载system("pause");return 0;}
result:horse_plus, age:30horse_plus, color: brownwhite+, age: 30+, color: brownwhiteoperator+, age: 30operator+, color : brownwhiteoperator+2, age: 110

运算符实现连续运算

在int,float等类型的加法运算中,是可以实现连加的,那么自定义类型的对象的连加如何实现?

  • 返回值必须是和运算符本身得到的结果的含义一致。如int+int,返回int,object+object,返回object。
  • 返回值必须是对象的引用,这样连续操作时是对原对象进行操作,才能在原基础上练加。
  • 当形参类型不同时,如果要实现操作数交换顺序,则需要进行运算符的函数重载。
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";Horse& operator+ (Horse& h1)		// 运算符+重载,返回引用{Horse result;this->age = this->age + h1.age;this->color = this->color + h1.color;return *this;					// this是对象本身的指针,*this是对象本身,返回值:自定义类型& 是对象本身的引用}};int main(){Horse h1;Horse h2;Horse h3;h1.age = 10;h2.age = 20;h3.age = 30;h1.color = "black";h2.color = "white";h3.color = "brown";cout << "+, age: " << (h1 + h2 + h3).age << endl;cout << "h1.color: " << h1.color << endl;cout << "+, color: " << (h1 + h2 + h3).color << endl;system("pause");return 0;
result:+, age: 60h1.color: blackwhitebrown+, color: blackwhitebrownwhitebrown
}
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";};Horse& operator+ (Horse& h1, Horse& h2){Horse result;h1.age = h1.age + h2.age;h1.color = h1.color + h2.color;return h1;}int main(){Horse h1;Horse h2;Horse h3;h1.age = 10;h2.age = 20;h3.age = 30;h1.color = "black";h2.color = "white";h3.color = "brown";cout << "+, age: " << (h1 + h2 + h3).age << endl;cout << "h1.color: " << h1.color << endl;cout << "+, color: " << (h1 + h2 + h3).color << endl;system("pause");return 0;}
result:+, age: 60h1.color: blackwhitebrown+, color: blackwhitebrownwhitebrown

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com