#include<iostream>
using namespace std;
class complex{
double real,imag;
public:
complex( double r= 0, double i= 0){
real=r;
imag=i;
}
complex operator+ (complex &c2);
void display();
};
complex complex:: operator+(complex &c2)
{
complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void complex::display(){
cout<<real<< " + "<<imag<< " i "<<endl;
}
int main()
{
complex a( 1, 2),b( 2, 3),c;
c=a+b;
c.display();
}
using namespace std;
class complex{
double real,imag;
public:
complex( double r= 0, double i= 0){
real=r;
imag=i;
}
complex operator+ (complex &c2);
void display();
};
complex complex:: operator+(complex &c2)
{
complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void complex::display(){
cout<<real<< " + "<<imag<< " i "<<endl;
}
int main()
{
complex a( 1, 2),b( 2, 3),c;
c=a+b;
c.display();
}
还有种更简单的方法,重载函数还可以这样写
complex complex::operator+(complex &c2)
{
return complex(real+c2.real,imag+c2.imag);
}
博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/
对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com
本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2011/12/18/2291605.html
,如需转载请自行联系原作者