C++ 中的运算符重载

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值