+号的重载

#include <iostream>
using namespace std;

class Complex {
	public:
		Complex(double r=0,double i=0):real(r),imag(i){}
		Complex operator + (Complex &c2) {
			Complex c;
			c.real=real+c2.real;
			c.imag=imag+c2.imag;
			return c;
		}
		void display();
	private:
		double real,imag;
};

void Complex::display()
{
	cout<<'('<<real<<','<<imag<<')'<<endl;
}
 
int main()
{
	Complex c1(1,1),c2(2,2);
	Complex c=c1+c2;
	c.display();
	return 0;
}
posted @ 2019-01-14 09:54  xyee  阅读(204)  评论(0编辑  收藏  举报