65,210
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class complex
{
double real;
double imag;
public:
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
friend complex operator +(complex c1, complex c2);
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};
complex operator +(complex c1,complex c2)
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
int main()
{
complex c1(3,4);
complex c2(2,5);
complex c_result = c1+c2;
c_result.display();
return 0;
}
#include<iostream>
using namespace std;
class complex
{
public:
double real;
double imag;
public:
complex();
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
complex(complex &complexIn);
complex operator + (complex c);
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};
complex::complex()
{
real = 0;
imag = 0;
}
complex::complex(complex &complexIn)
{
this->real = complexIn.real;
this->imag = complexIn.imag;
}
complex complex::operator + (complex c)
{
complex tmpResult(0, 0);
tmpResult.real = this->real + c.real;
tmpResult.imag = this->imag + c.imag;
return tmpResult;
}
int main()
{
complex c1(3, 4);
complex c2(2, 5);
complex c_result = c1 + c2;
c_result.display();
return 0;
}
class complex
{
double real;
double imag;
public:
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
friend complex operator +(complex c1, complex c2) //是Vc6的话,友员的声明跟定义都放在类体里
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};
int main()
{
complex c1(3,4);
complex c2(2,5);
complex c_result = c1+c2;
c_result.display();
return 0;
}