您的位置:首页 > 编程语言 > C语言/C++

【C++】操作符重载及实现简单的复数类Complex

2017-05-18 00:50 441 查看
操作符重载:

重载操作符是具有特殊函数名的函数,关键字operator后面接需要定义的操作符符号。

操作符重载也是一个函数,具有返回值和形参表。它的形参数目与操作符的操作数目相同。

函数调用操作符可以接受任意数目的操作数。

使用运算符重载可以提高代码的可读性。

返回类型 operate 操作符(参数列表);



不可重载的操作符






注意:

1、不能通过连接其他符号来创建新的操作符:比如operator@;void operator @(){}

2、重载操作符必须有一个类类型或者枚举类型的操作数

int operator +(const int _iNum1 , const int _iNum2 )

// 报错

{

return ( _iNum1 + _iNum2);

} t

ypedef enum TEST {one ,two ,three };

int operator+(const int _iNum1 , const TEST _test )

{

return _iNum1;

}

3、用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义

5、不再具备短求职特性重载操作符不能保证操作符的求值顺序,在重载&&和||中,对每个操作数都要进行求值,而且对操作数的求职顺序不能做规定,因此:重载&&、||和逗号操作符不是好的做法。

6、作为类成员的重载函数,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参。

CTest operator+(const CTest test1, const CTest

test2)const // 报错

{

return test1;

} C

Test operator+(const CTest test1)const

{

return test1;

}

7、一般将算术操作符定义为非成员函数,将赋值运算符定义成员函数

8、操作符定义为非类的成员函数时,一般将其定义为类的友元

9、== 和 != 操作符一般要成对重载

10、下标操作符[]:一个非const成员并返回引用,一个是const成员并返回引用

11、解引用操作符*和->操作符,不显示任何参数

13、自增自减操作符前置式++/--必须返回被增量或者减量的引用后缀式操作符必须返回旧值,并且应该是值返回而不是引用返回

14、输入操作符>>和输出操作符<<必须定义为类的友元函数

【建议】

使用重载操作符,可以令程序更自然、更直观,而滥用操作符重载会使得类难以理解,在实践中很少发生明显的操作符重载滥用。但有些程序员会定义operator+来执行减法操作,当一个重载操作符不明确时,给操作符取一个名字更好,对于很少用的操作,使用命名函数通常比用操作符好,如果不是普通操作,没有必要为简洁而用操作符。

实现一个复数类的以下接口:
//运算符重载

Complex& operator=(const Complex& d);//赋值

bool operator ==(const Complex& d);//判断相等
bool operator !=(const Complex& d);//判断相等
Complex operator+(const Complex& d);//复数相加

Complex& operator+=(const Complex& d); //复数对象+=d

Complex operator-(const Complex& d); //两个复数相减

Complex& operator-=(const Complex& d); //复数对象-=d

Complex operator++(); //前置++

Complex operator++(int); //后置++ 且不用添加参数 特殊形式

Complex operator--(); //前置--

Complex operator--(int); //后置--

void Display(); //显示复数的实部和虚部
具体实现代码:
“Complex.h”

#include<iostream>;
using namespace std;

class Complex
{
public:
Complex(int real=0, int image=0)
:_real(real)
, _image(image)
{}

~Complex()
{}

Complex(Complex& com)
:_real(com._real)
,_image(com._image)
{}

//运算符重载
Complex& operator=(const Complex& d)//赋值
{
_real = d._real;
_image = d._image;
return *this;
}
bool operator ==(const Complex& d)//判断相等
{
return (_real == d._real) && (_image == d._image);
}

bool operator ==(const Complex& d)//判断不相等
{
return (_real != d._real) || (_image != d._image);
}

Complex operator+(const Complex& d)//复数相加
{
Complex temp(0, 0);
temp._real = _real + d._real;
temp._image = _image + d._image;
return temp;
}
//Complex& operator+(const Complex& d)//复数相加
//{
//	_real=_real+d._real;
//	_image = _image + d._image;
//	return *this;
//}
Complex& operator+=(const Complex& d)  //复数对象+=d
{
_real += d._real;
_image += d._image;
return *this;
}
Complex operator-(const Complex& d)  //两个复数相减
{
Complex temp(0, 0);
temp._real = _real - d._real;
temp._image = _image - d._image;
return temp;
}
Complex& operator-=(const Complex& d)  //复数对象-=d
{
_real -= d._real;
_image -= d._image;
return *this;
}
Complex operator++()  //前置++
{
_real++;
_image++;
return *this;
}
Complex operator++(int)  //后置++  且不用添加参数  特殊形式
{
Complex temp = *this;
_real++;
_image++;
return temp;
}
Complex operator--() //前置--
{
_real--;
_image--;
return *this;
}
Complex operator--(int)  //后置--
{
Complex temp = *this;
_real--;
_image--;
return temp;
}
void Display()  //显示复数的实部和虚部
{
cout << "real:" << _real << endl;
cout << "image:" << _image << endl;
}
private:
int _real;
int _image;
};


"Complex.cpp"

#include"Complex.h"

int main()
{
Complex d1(5, 10);
Complex d2(7, 10);
Complex d4;

cout << "d1(5,10)  "<<"d2(7,10)  "<<"d4(0,0)"<<endl;

cout << "d4 = d1" << endl;
d4 = d1;
d4.Display();
cout << "d2 - d1" << endl;
d4 = d2 - d1;
d4.Display();
cout << "d2 + d1" << endl;
d4 = d2 + d1;
d4.Display();
cout << "d4 += d1" << endl;
d4 += d1;
d4.Display();
cout << "d4 -= d1" << endl;
d4 -= d1;
d4.Display();
cout << "d4++" << endl;
d4++;
d4.Display();
cout << "++d4" << endl;
++d4;
d4.Display();
cout << "d4++" << endl;
d4--;
d4.Display();
cout << "--d4" << endl;
--d4;
cout << "d4 == d1" << endl;
cout << (d4 != d1) << endl;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息