#include <iostream>
using namespace std;
class point
{
private:
float real, imag;
public:
point ( float a = 0, float b = 0)
{
real = a;
imag = b;
}
void print ()
{
cout<<real<<" "<<imag<<endl;
}
friend point operator + ( point &, point &);
friend point operator ++ ( point &);
friend point operator ++ ( point &, int); //后自增
};
point operator + ( point & A, point & B)
{
return point ( A.real + B.real,A.imag + B.imag );
}
point operator ++ ( point & A)
{
cout<<"前自增"<<endl;
A.real += 1;
A.imag += 1; //这里 自增 要这么搞,, 不能 这样 return A( A.real += 1, A.imag += 1);这样 对象 不改变 不知道为什么。。。
return A;
}
point operator ++ ( point &C, int)
{
cout<<"后自增"<<endl;
point temp = C;
++C;
return temp;
}
int main()
{
point A ( 1.5, 5.5 ), B(2.5, 6.5), C;
C = A + B; //相当于 A.operator + ( B )
C.print ();
A =++A;
A.print ();
C = B++;
B.print ();
C.print ();
return 0;
}
以成员函数形式 重载运算符
最新推荐文章于 2024-09-29 13:24:20 发布