深入理解运算符重载

运算符重载就是函数

自定义类的 赋值运算符重载函数的作用与内置纷纷投赋值运算符的作用类似,但是要注意的是,它与 拷贝构造函数析构函数一样,要注意深拷贝浅拷贝的问题,在没有深拷贝浅拷贝的情况下,如果没有指定默认的赋值运算符重载函数,那么系统将会自动提供一个赋值运算符重载函数。

运算符重载的声明方式与方法的声明方式相同,但operator 关键告诉 编译器,它实际上是一个运算符重载,后面是相关运算符的符号,在本例中就是+。返回类型是在使用这个运算符时获得的类型。在本例中,把两个矢量加起来会得到另一个矢量,所以返回类型就是Vector。对于这个+运算符重载,返回类型与包含类一样,但这种情况并不是必需的。两个参数就是要操作的对象。对于二元运算符(带两个参数),如+和-运算符,第一个参数是放在运算符左边的值,第二个参数是放在运算符右边的值。

*运算符重载:

1.运算符重载就是赋予已有的运算符多重含义,即多种功能。

2.运算符重载的目的:通过运算符的重载即重新定义使得其能够用于特定类的对象执行特定的功能。

3.对于运算符的重载首先要关心的就是那些运算符能够重载,那些不能重载;

能够重载的运算符:

  (1.算术运算符:+-*/%++--

  (2)位操作运算符:&|^~<<>>

  (3)逻辑运算符:!,&&||

  (4)比较运算符:><>=<===!=

  (5)赋值运算符:=+=-=*=/=%=&=|=^=~=<<=>>=

  (6)其他运算符:[],(),->,’,newdeletenew[],delete[],->*

不能重载的运算符:

..*,::,?:

4.运算符重载后不影响其结合性和优先级,切不改变其操作数的个数及语法结构。(即运算符重载后单目运算符仍旧为单目运算符,双目运算符仍旧为单目运算符)

5.重载运算符的限制条件:

   (1.不能妄造新的运算符,必须把重载运算符限制在C++语言中已有的运算符范围内允许重载的运算符内。

   (2).重载运算符的四个“不能改变”:

       a.不能改变运算符操作数的个数;

       b.不能改变运算符原有的优先级;

       c.不能改变运算符原有的结合性;

       d.不能改变运算符原有语法结构;

6.运算符重载必须遵循的原则:

运算符重载可以使得程序更加简洁,使表达式更加直观,增强可读性,但是不可过多的使用,否则会适得其反。

   (1.重载的运算符含义必须清楚:这个程序中,加法(+)运算用于Time的对象,含义不清,所以给Time类不能重载运算符+

class Time
{
public:
 Time()
  { hours = minutes = seconds = 0; }
  Time(int h, int m, int s)
  {
   hours = h; minutes = m; seconds = s;
  }
private:
 int hours, minutes, seconds;
};
Time t1(8, 10, 20), t2(9, 15, 30), t3;
     t3 = t1 + t2;

   2.重载运算符不能有二义性:

   (3)在定义运算符时必须含义准确,用法确定,不可含糊不清,在同一个地方一种重载运算符只能拥有一种理解。*运算符重载函数的两种形式:

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




void print(const complex &c)
{
 if (c.image < 0)
  cout << c.real << c.image << 'i';
 else
  cout << c.real << '+' << c.image << 'i';
}

       . . . . . . . . . . . . . .  (主函数)


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


void print(const complex &c)
{
 if (c.image < 0)
  cout << c.real << c.image << 'i';
 else
  cout << c.real << '+' << c.image << 'i';
}

        . . . . . . .  . . . 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章