#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double r,double i):real(r),imag(i){}
friend Complex operator +(Complex &s1,Complex &s2);
friend Complex operator +(int &i,Complex &s1);
Complex operator +(int &i);
void display();
private:
double real;
double imag;
};
Complex operator +(Complex &s1,Complex &s2)
{
return(Complex(s1.real+s2.real,s1.imag+s2.imag));
}
Complex operator +(int &i,Complex &s1)
{
return(Complex(i+s1.real,s1.imag));
}
Complex Complex::operator +(int &i)
{
return(Complex(real+i,imag));
}
void Complex::display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
int main()
{
int i=10;
Complex c1(4.0,3.0),c2(2.0,1.0);
Complex c3=c1+c2;
c3.display();
c3=i+c1;
c3.display();
c3=i+c2;
c3.display();
}
3
最新推荐文章于 2023-01-30 08:34:38 发布