标签:深入理解运算符的重载
*运算符重载:
1.运算符重载就是赋予已有的运算符多重含义,即多种功能。
2.运算符重载的目的:通过运算符的重载即重新定义使得其能够用于特定类的对象执行特定的功能。
3.对于运算符的重载首先要关心的就是那些运算符能够重载,那些不能重载;
能够重载的运算符:
(1).算术运算符:+,-,*,/,%,++,--;
(2)位操作运算符:&,|,^,~,<<,>>;
(3)逻辑运算符:!,&&,||;
(4)比较运算符:>,<,>=,<=,==,!=;
(5)赋值运算符:=,+=,-=,*=,/=,%=,&=,|=,^=,~=,<<=,>>=;
(6)其他运算符:[],(),->,’,new,delete,new[],delete[],->*。
不能重载的运算符:
.,.*,::,?:
4.运算符重载后不影响其结合性和优先级,切不改变其操作数的个数及语法结构。(即运算符重载后单目运算符仍旧为单目运算符,双目运算符仍旧为单目运算符)
5.重载运算符的限制条件:
(1).不能妄造新的运算符,必须把重载运算符限制在C++语言中已有的运算符范围内允许重载的运算符内。
(2).重载运算符的四个“不能改变”:
a.不能改变运算符操作数的个数;
b.不能改变运算符原有的优先级;
c.不能改变运算符原有的结合性;
d.不能改变运算符原有语法结构;
6.运算符重载必须遵循的原则:
运算符重载可以使得程序更加简洁,使表达式更加直观,增强可读性,但是不可过多的使用,否则会适得其反。
(1).重载的运算符含义必须清楚:这个程序中,加法(+)运算用于Time的对象,含义不清,所以给Time类不能重载运算符+。
Eg: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.重载为类的成员函数:
Eg:
#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.重载为类的友员函数:
Eg:
#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‘;
}
. . . . . . . . . . . . . . . .(主函数)
标签:深入理解运算符的重载
原文地址:http://10919090.blog.51cto.com/10909090/1758497