定义重载运算符函数:opereator@(),其中@表示运算符号
#include<iostream>
using namespace std;
class Complex{
public:
double real;
double imag;
Complex(double r=0,double i=01)
{
real=r;
imag=i;
}
};
Complex operator+(Complex com1,Complex com2)
{
Complex temp;
temp.real=com1.real+com2.real;
temp.imag=com1.imag+com2.imag;
return temp;
}
int main()
{
Complex cp(3.5,2.5),cp1(6.5,5.5),total1,total2;
total1=operator+(cp,cp1);//显示调用运算符重载函数opreator+()
cout<<"real1="<<total1.real<<" imag1="<<total1.imag<<endl;
total2=cp+cp1;//隐式调用运算符重载函数operator+()
cout<<"real2="<<total2.real<<" imag2="<<total2.imag<<endl;
return 0;
}