#include<iostream>
#include<stdio.h>
using namespace std;
class my_complex
{
public:
my_complex& operator ++ ();//前置++
my_complex& operator ++ (int);//后置++
public: //单目运算符重载
my_complex& operator + ();
my_complex& operator - ();
my_complex* operator & ();
my_complex& operator * ();
public: //一般运算符的重载,成员函数
my_complex operator +(my_complex &sd);
my_complex operator - (my_complex &sd);
my_complex operator * (my_complex &sd);
my_complex operator / (my_complex &sd);
/* //友元函数
friend complex operator +(const complex &c1, const complex &c2);
friend complex operator -(const complex &c1, const complex &c2);
friend complex operator *(const complex &c1, const complex &c2);
friend complex operator /(const complex &c1, const complex &c2);
*/
public :
void get_data(void )
{
cout << "real = "<< real<<endl;
cout << "virt = "<< virt << endl;
}
private:
double real;
double virt;
public:
my_complex(){}
my_complex(double x,double y)
{
real = x;
virt = y;
}
my_complex (my_complex & sd)
{
real = sd.real;
virt = sd.virt;
}
my_complex operator = (my_complex &sd)
{
if(this != &sd)
{
real = sd.real;
virt = sd.virt;
}
return *this;
}
~my_complex()
{}
};
inline my_complex my_complex::operator +(my_complex &sd)
{
real = real +sd.real;
virt = virt + sd.virt;
my_complex temp(real,virt) ;
return temp;
//return my_complex::my_complex(real+ sd.real,virt + sd.virt);
}
inline my_complex my_complex::operator /(my_complex &sd )
{
real = real / sd.real;
virt = virt /sd.virt;
my_complex temp(real,virt);
return temp;
}
/********************************************************************************/
my_complex& my_complex:: operator + ()
{
;
return *this;
}
my_complex& my_complex ::operator - ()
{
this ->real = - real;
this ->virt = - virt;
return *this;
}
my_complex* my_complex ::operator & ()
{
return this;
}
my_complex& my_complex::operator * ()
{
return *this;
}
/********************************************************************************/
my_complex& my_complex::operator ++ () //前置++
{
this -> real = real + 1;
this -> virt = virt + 1;
return *this;
}
my_complex& my_complex ::operator ++ (int) //后置++
{
my_complex temp(real,virt) ;
real = real+ 1;
virt = virt + 1;
return temp;
}
/**************************************************************************************/
int main()
{
my_complex a (1.2,2.4);
my_complex b (0.8,1.6);
a++;
++a;
a.get_data();
b.get_data();
my_complex c ;
c = -(a /b);
cout << &c<<endl;
(*(&c)).get_data();
return 0;
}
运行结果:
real = 3.2
virt = 4.4
real = 0.8
virt = 1.6
0018FF0C
real = -4
virt = -2.75
Press any key to continue