运算符重载作用 您所在的位置:网站首页 什么叫运算符的优先级 运算符重载作用

运算符重载作用

2023-12-20 21:31| 来源: 网络整理| 查看: 265

运算符重载是为了解决类对象之间的运算的,通常的运算符只用于算术运算,如常量int之间,因为编译器已经定义了;而一个类两个对象之间成员进行运算必须重新定义,让编译器在遇到对象运算时能按我们要求的进行运算,这就是运算符重载的意义,即重定义运算符,因此你可以看到,运算符重载就是为类对象服务的,那么两个对象的成员进行运算那必须先获得对象本身啦,所以运算符重载参数必须含有类指针或引用,这是主要客户。 

      运算符重载的声明operator 关键字告诉 编译器,它是一个运算符重载,后面是相关运算符的符号,如+。返回类型是在使用这个运算符时获得的类型。对于这个+运算符重载,返回类型与包含类一样,但这种情况并不是必需的。两个参数就是要操作的对象。对于二元运算符(带两个参数),如+和-运算符,第一个参数是放在运算符左边的值,第二个参数是放在运算符右边的值。

    1.重载为类的成员函数  

1.重载为类的成员函数 #include class complex { public: complex() { real = image = 0; } complex(double r, double i) { real = r; image = i; } complex operator+(const complex&c); complex operator-(const complex&c); complex operator*(const complex&c); complex operator/(const complex&c); friend void printf(const complex&c); private: double real, image; }; inline complex complex::operator+(const complex& c) { return complex(real + c.real, image + c.image); } inline complex complex::operator-(const complex&c) { return complex(real - c.real, image - c.image); } inline complex complex::operator*(const complex&c) { return complex(real*c.real - image*c.image, real*c.image + image*c.real); } inline complex complex::operator/(const complex&c) { return complex((real*c.real + image*c.image) / (c.real*c.real+c.image*c.image), (image*c.real - real*c.image) / (c.real*c.real + c.image*c.image)); } 2.重载为类的友员函数 #include class complex { public: complex() { real = image = 0; } complex(double r, double i) { real = r; image = i; } friend complex operator+(const complex&c1,const complex&c2); friend complex operator-(const complex&c1, const complex&c2); friend complex operator*(const complex&c1,const complex&c2); friend complex operator/(const complex&c1,const complex&c2); friend void printf(const complex&c); private: double real, image; }; inline complex complex::operator+(const complex& c1,const complex&c2) { return complex(c1.real + c2.real,c1.image + c2.image); } inline complex complex::operator-(const complex&c1,const complex&c2) { return complex(c1.real - c2.real, c1.image - c2.image); } inline complex complex::operator*(const complex&c1,const complex&c2) { return complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image +c1.image*c2.real); } inline complex complex::operator/(const complex&c1,const complex&c2) { return complex((c1.real*c2.real + c1.image*c2.image) / (c2.real*c2.real+c2.image*c2.image), (c1.image*c2.real - c1.real*c2.image) / (c2.real*c2.real + c2.image*c2.image)); }

版权声明:本文为CSDN博主「AI小丸子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_22424571/article/details/82961597



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有