C++ 中的运算符重载

来源:互联网 发布:图片文字编辑器软件 编辑:程序博客网 时间:2024/06/07 14:51

#include<iostream>#include<stdio.h>using namespace std;class   my_complex{public:                         //关系运算符重载    bool operator == (const A& );     bool operator != (const A& );    bool operator <  (const A& );    bool operator <= (const A& );    bool operator >  (const A& );    bool operator >= (const A& ); public:                           //逻辑运算符重载    bool operator || (const A& );    bool operator && (const A& );    bool operator ! ();public:                           //单目运算符重载    my_complex& operator + ();    A& operator - ();    A* operator & ();    //A& operator * ();public:                             //自增运算符重载     A& operator ++ ();//前置++     A operator  ++ (int);//后置++     A& operator --();//前置--     A operator  -- (int);//后置--public:    A operator |  (const A& );    A operator &  (const A& );    A operator ^  (const A& );    A operator << (int i);    A operator >> (int i);    A operator ~ ();public:                       //赋值运算符的重载    A& operator += (const A& );    A& operator -= (const A& );     A& operator *= (const A& );    A& operator /= (const A& );    A& operator %= (const A& );    A& operator &= (const A& );    A& operator |= (const A& );    A& operator ^= (const A& );    A& operator <<= (int i);    A& operator >>= (int i);public:                          //内存运算符的重载    void *operator new(size_t size);    void *operator new(size_t size, int i);    void *operator new[](size_t size);    void operator delete(void*p);    void operator delete(void*p, int i, int j);    void operator delete [](void* p);public:     /*   特殊的运算符重载  下面的运算符只能用一种,特殊吧。 这些运算符的重载只能是成员函数 */    A& operator = (const A& );    char operator [] (int i);//返回值不能作为左值    const char* operator () ();    T operator -> ();          /*下面的类型转换符*/    operator char* () const;    operator int ();    operator const char () const;    operator short int () const;    operator long long () const;public:        /*而这些只能以友元函数的形式重载*/    friend inline ostream &operator << (ostream&, A&);//输出流    friend inline istream &operator >> (istream&, A&);//输入流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& operator + (){}    A& operator - ();    A* operator & ();    A& operator * ();int main(){    my_complex a (1.2,2.4);my_complex b (0.8,1.6);a.get_data();b.get_data();    my_complex c ;c = a /b;c.get_data();return 0;}

0 0