c++为什么要进行运算符重载?

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

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

      实例:

1.重载为类的成员函数

#include<iostream.h>
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<iostream.h>

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));
}

 

  • 16
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ai智享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值