[C++面向对象程序设计]第四章 对运算符进行重载

1.

#include<iostream>
using namespace std;
class Complex
{
public:
	int real1(){return real;};
	int image1(){return image;};
	Complex(int x,int y):real(x),image(y){}
	void display(){cout<<real<<","<<image<<endl;}
private:
	int real;
	int image;
};

Complex operator+(Complex &c1,Complex &c2)
{return Complex(c1.real1()+c2.real1(),c1.image1()+c2.image1());}

int main()
{
	Complex c1(2,3);
	Complex c2(2,3);
	Complex c3(2,3);
	c3=c1+c2;
	c3.display();
	return 0;
}



猜你喜欢

转载自blog.csdn.net/daisy_fight/article/details/80185372