C++一些常见的运算符重载

#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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值