C++ 运算符重载

   将现在的操作符与一个成员函数相关联并将该操作与其类的对象(作为它的操作数)一起使用,称为操作符重载。重载运算符用operator关键字来完成。

操作符函数的格式如下:

operator op(argument-list)

其中,op是将要重载的操作符,例如,operator+()重载+操作符。operator *()重载*操作符。op必须是有效的C++操作符,不能虚构一个新的符号。

1,重载运算符其实就是重载函数,它的优先级和结合性是不能被更改,但是参数和返回值都是可以更改的。
2, c++规定  .  ->   ::   *    ?: 这五个运算符不能重载, .成员运算符。 ->指针成员运算符。 *间接运算符。?:条件去处符。
 也不能创造新的运算符。
3,在重载运算时,应保持重载生的运算意义与原行的运算相一致,不允许出现改变原先运算符意义的重载。

三目运算符不能重载
单目运算符重载。
openater++()//前置方法
openater++(int)//后置方法。int是说明符,并不是数据类型。。单目运算符是没有参数的。

#include <iostream>

class CPoint

{

private:

int nPointX,nPointY;

public:

CPoint(int _nX=0,int _nY=0)

{

nPointX=_nX;

nPointY=_nY;

}

void SetPoint(int _nX,int _nY)

{

nPointX=_nX;

nPointY=_nY;

}

int GetPointX() {return nPointX;}

int GetPointY() {return nPointY;}

CPoint operator+(CPoint &_obj)

{

int nX=nPointX+_obj.GetPointX();

int nY=nPointY+_obj.GetPointY();

return CPoint(nX,nY);

}

void Show()

{

std::cout<<"nPointX:"<<nPointX<<std::endl;

std::cout<<"nPointY:"<<nPointY<<std::endl;

}

};


int main(int argc, const char * argv[])

{


    // insert code here...

CPoint pointA(10,20);

CPoint pointB(20,20);

CPoint pointC;

pointC=pointA+pointB;

pointC.Show();

    

    return 0;

}

《用普通函数的形式重载运算符》

#include <iostream>

class CPoint

{

private:

int m_nPointX,m_nPointY;

public:

CPoint(int _nX=0,int _nY=0)

{

m_nPointX=_nX;

m_nPointY=_nY;

}

void SetPoint(int _nX,int _nY)

{

m_nPointX=_nX;

m_nPointY=_nY;

}

void Show()

{

std::cout<<"m_nPointX="<<m_nPointX<<std::endl;

std::cout<<"m_nPointY="<<m_nPointY<<std::endl;

}

};


std::ostreamoperator<<(std::ostream& _out,CPoint& _obj)

{

std::cout<<"In operator << ()"<<std::endl;

_obj.Show();

return _out;

}


std::istreamoperator>>(std::istream& _in,CPoint& _obj)

{

int nX,nY;

nX=nY=0;

std::cout<<"Please input point X:";

_in>>nX;

std::cout<<"Please input point Y:";

_in>>nY;

    

_obj.SetPoint(nX,nY);

return _in;

}


int main(int argc, const char * argv[])

{


    // insert code here...

CPoint pointA(10,20);

CPoint pointB(20,20);

CPoint pointC;

std::cout<<"Point pointA:"<<std::endl;

std::cout<<pointA;

std::cout<<"Point pointB:"<<std::endl;

std::cout<<pointB;

std::cout<<"Input pointC:"<<std::endl;

std::cin>>pointC;

std::cout<<pointC;

    

    return 0;


}



单目运算符重载。
openater++()//前置方法
openater++(int)//后置方法。int是说明符,并不是数据类型。。单目运算符是没有参数的。

#include <iostream>

class Clock

{

private:

int Hour,Minute,Second;

public:

Clock(int _h=0,int _m=0,int _s=0)

{

Hour=_h;

Minute=_m;

Second=_s;

}

void ShowTime()const;

void operator ++();

void operator ++(int);

};


void Clock::operator++()

{

Second++;

if(Second>=60)

{

Second-=60;

Minute++;

if (Minute>=60)

{

Minute-=60;

Hour++;

Hour%=24;

}

}

std::cout<<"++Clock:";

}


void Clock::operator++(int)

{

Second+=1;

if(Second>=60)

{

Second-=60;

Minute++;

if (Minute>=60)

{

Minute-=60;

Hour++;

Hour%=24;

}

}

std::cout<<"Clock++:";

}


void Clock::ShowTime()const

{

std::cout<<Hour<<":"<<Minute<<":"<<Second<<std::endl;

}


int main(int argc, const char * argv[])

{


    // insert code here...

 Clock myClock(23,59,59);

std::cout<<"First time output:";

myClock.ShowTime();

    myClock++;

myClock.ShowTime();

++myClock;

myClock.ShowTime();

   

    return 0;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值