重载运算符和输入输出流
前几天看到重载有点忘记了,所以翻翻书写了个重载,运算符实际上是特殊的函数,C++语言允许有程序员重新定义运算符的语义,这一机制称为运算符重载。运算符函数通常分为两种形式:一类
是在类中定义的运算符成员函数,称之为类成员运算符,另一种在类之外定义的运算符重载,通常以该类的友元形式,称之为友元运算符。C++只允许重载一员或者二元运算符。
一般形式为:
类型 类名::operator 运算符 (参数表)
{
........//运算符函数体
}
接下来就是一些细节问题了,在重载除号与减号的时候,必须区分左右操作数,如 CComplex operator/ (CComplex & other);,this所指向的便是左操作数,other指向的是右操作数,起初我以为重载为什么一定要用类呢?于是我没用类的思想,直接用C的形式写了。结果程序果断报错,最后我翻翻资料发现,C语言不能实现运算符的重载!(哈哈,目前尚无文献考证,只是上网搜了一下,不过我试过,也不行就是鸟)。
再讲一下重载输入输出流,重载输入输出流一般是以类的友元形式进行
一般形式为:
class CComplex
{
public:
friend ostream& operator<< (ostream & stream,CComplex & other);
friend istream& operator>>(ostream & stream,CComplex &other);
};
ostream& operator<< (ostream& stream,CComplex &other)//重载输出流
{
if(other.real !=0 && other.image != 0) stream<<other.real<<"+"<<other.image<<"i"<<endl;
if(other.real ==0 && other.image != 0) stream<<other.image<<"i"<<endl;
if(other.real !=0 && other.image == 0) stream<<other.real<<endl;
if(other.real ==0 && other.image == 0) stream<<"0"<<endl;
return stream;
}
istream& operator>>(ostream & stream,CComplex &other)//重载输入流
{
cout<<"input real part"<<endl;
stream>>other.real;
cout<<"input image part"<<endl;
stream>>other.real;
return stream;
}
再把我自己写的重载加减乘除和输出<<流的完整代码贴上来:
#include<iostream.h>
class CComplex
{
public:
CComplex(double x = 0, double y = 0);
CComplex operator+ (CComplex & other);
CComplex operator- (CComplex & other);
CComplex operator* (CComplex & other);
CComplex operator/ (CComplex & other);
CComplex operator= (CComplex & other);
friend ostream & operator<<(ostream & stream,CComplex &other);
void Printf();
double real;
double image;
};
CComplex::CComplex(double x, double y)
{
real = x;
image = y;
}
CComplex CComplex::operator+ (CComplex & other)
{
CComplex temp;
temp.real = real+other.real;
temp.image = image+other.image;
return temp;
}
CComplex CComplex::operator- (CComplex & other)//注意此处other是右操作数
{
CComplex temp;
temp.real = real-other.real;
temp.image = image-other.image;
return temp;
}
CComplex CComplex::operator*(CComplex & other)
{
CComplex temp;
temp.real = real * other.real - image * other.image;
temp.image = other.real * image + real* other.image;
return temp;
}
CComplex CComplex::operator/(CComplex & other)
{
CComplex temp;
temp.real = (real * other.real + image * other.image ) / (other.real * other.real + other.image * other.image);
temp.image = (image * other.real - real * other.image )/(other.real * other.real + other.image * other.image);
return temp;
}
CComplex CComplex::operator= (CComplex & other)
{
image = other.image;
real = other.real;
return *this;
}
void CComplex::Printf()
{
if(real !=0 && image != 0) cout<<real<<"+"<<image<<"i"<<endl;
if(real ==0 && image != 0) cout<<image<<"i"<<endl;
if(real !=0 && image == 0) cout<<real<<endl;
if(real ==0 && image == 0) cout<<"0"<<endl;
}
ostream& operator<< (ostream& stream,CComplex &other)
{
if(other.real !=0 && other.image != 0) stream<<other.real<<"+"<<other.image<<"i"<<endl;
if(other.real ==0 && other.image != 0) stream<<other.image<<"i"<<endl;
if(other.real !=0 && other.image == 0) stream<<other.real<<endl;
if(other.real ==0 && other.image == 0) stream<<"0"<<endl;
return stream;
}
int main()
{
double r1,i1,r2,i2;
cout<<"请按次序输入第一个复数和第二个复数的实部和虚部"<<endl;
cin>>r1>>i1>>r2>>i2;
CComplex x(r1,i1),y(r2,i2);
CComplex sum1,sum2,sum3,sum4;
sum1 = x + y;
sum2 = x - y;
sum3 = x * y;
sum4 = x / y;
sum1.Printf();
sum2.Printf();
sum3.Printf();
sum4.Printf();
cout<<"重载输出如下"<<endl;
cout<<sum1;
cout<<sum2;
cout<<sum3;
cout<<sum4;
return 0;
}
此程序在VC6.0下通过编译
如果你发现任何错误,请留言!谢谢,我将尽快回复*************