#include<iostream>
using namespace std;
class complex{
private:
double real,virt;
public:
complex(double r=0.0,double v=0.0){
real=r;
virt=v;
}
void display(){
cout<<"("<<real<<", "<<virt<<"i)"<<endl;
}
friend complex operator + (complex &,int);
friend complex operator + (int,complex &);
};
complex operator + (complex &a,int b){
complex temp;
temp.real=a.real+b;
temp.virt=a.virt;
return temp;
}
complex operator + (int b,complex &a){
complex temp;
temp.real=a.real+b;
temp.virt=a.virt;
return temp;
}
int main()
{
double r,v,a,b;
cout<<"输入复数C的实部和虚部"<<endl;
cin>>r>>v;
complex A(r,v),C,D;
cout<<"输入整数a和b"<<endl;
cin>>a>>b;
C=A+a;
D=b+A;
cout<<"复数C=";A.display();
cout<<"复数C+整数a=";C.display();
cout<<"整数b+复数C=";D.display();
return 0;
}
使用友元运算符重载函数实现一个复数与一个整数的相加、一个整数与一个复数的相加
最新推荐文章于 2023-04-17 11:31:12 发布