运算符重载

#include<iostream>
using namespace std;

class Complex
{
public:
	Complex() {real=0;imag=0;}
	Complex(double r,double i) {real=r;imag=i;}
	double get_real() {return real;}
	double get_imag() {return imag;}
	void display() {cout<<"("<<real<<","<<imag<<"i)"<<endl;}
private:
	double real;
	double imag;
};

Complex operator+(Complex &c1, Complex &c2)
{
	return Complex(c1.get_real() + c2.get_real(), c1.get_imag() + c2.get_imag());
}


void main()
{
	Complex c3(3,5),c5;
	Complex c4(2,4);
	c5 = c3 + c4;
	cout << "c5=" ;
	c5.display();
}

 

posted on 2022-10-26 20:48  进取  阅读(14)  评论(0编辑  收藏  举报