运算符重载
#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();
}