C++ 运算符重载


#include 
using namespace std;


class Complex
{
public:
	double real,imag;
public:
	Complex(){
		real=0;
		imag=0;
	}
	Complex(double r,double i)
	{
		real=r;
		imag=i;
	}
	Complex add(Complex & x)
	{
		Complex temp;
		temp.real=real+x.real;
		temp.imag=imag+x.imag;
		return temp;
	}
	Complex* add2(Complex *x)
	{
		Complex *temp=new Complex;
		temp->imag=imag+x->imag;
		temp->real=real+x->real;
		return temp;
	}
	//operator后跟运算符
	Complex operator + (Complex &x)
	{
		Complex temp;
		temp.real=real+x.real;
		temp.imag=imag+x.imag;
		return temp;
	}
};


int main()
{
	Complex a(1,2),b(3,4),c;
	c=a.add(b);
	Complex *d,*e;
	e=new Complex(5,6);
	d=a.add2(e);
	cout<imag<

© 2013, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记