复数加减(运算符重载)
- 声明一个复数类CComplex(类私有数据成员为double型的real和image)
- 定义构造函数,用于指定复数的实部与虚部。
- 重载<<运算符,以格式real+image i的格式输出当前对象(当虚部为非负数时,实部虚部中间用+号连接,当虚部为负数时,实部虚部用-号连接:如3+4i,3-4i,3+0i)。
- 重载+运算符,实现两个复数对象之和(实部虚部分别相加)。
- 重载-运算符,实现两个复数对象的差(实部虚部分别相减)。
#include <iostream>
using namespace std;
class CComplex
{
public:
CComplex(){}
CComplex(double _r,double _i)
{
real=_r;
imag=_i;
}
friend istream &operator <<(istream & ,CComplex & );
friend ostream &operator >>(ostream & ,CComplex & );
CComplex operator+(CComplex c) {
return CComplex(real+c.real,imag+c.imag);
}
CComplex operator-(CComplex c){
return CComplex(real-c.real,imag-c.imag);
}
double real,imag;
};
istream &operator >>(istream & is,CComplex &c){
is>>c.real>>c.imag;
return is;
}
ostream &operator <<(ostream & os ,CComplex& c ){
os<<c.real<<showpos<<c.imag<<"i"<<noshowpos<<endl;
return os;
}
int main() {
double r1,r2,i1,i2;
cin>>r1>>i1;
cin>>r2>>i2;
CComplex c1(r1,i1),c2(r2,i2),c3,c4;
cout<<c1;
cout<<c2;
c3=c1+c2;
cout<<c3;
c4=c1-c2;
cout<<c4;
return 0;
}
标签:real,return,CComplex,imag,C++,double,operator From: https://www.cnblogs.com/zh-ang-zhang/p/17391739.html