运算符重载

一句话总结:究其本质,触类旁通。特别注意后置++/--的实现,如前置++和后置++的本质实现过程与重载的实现方法。

一元运算符重载:负、++、-- 运算符重载

二元运算符重载:加减

输出运算符重载

//  COperator.hpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/18.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//


#ifndef COperator_hpp

#define COperator_hpp


#include <stdio.h>

#include <iostream>


class COperator

{

    friend COperator &operator--(COperator &c);//前置--

    friend COperator operator--(COperator &c, int);//int是一个标志,告诉编译器这是一个后置--,需放在最后

    friend COperator &operator-(const COperator c1, const COperator c2);//二元减运算符

    friend std::ostream &operator<<(std::ostream &out, COperator &c);//输出运算符,不可为成员返回,属于ostream对象

    

public:

    COperator();

    ~COperator();

    /*一元运算符*/

    COperator &operator-();

    COperator &operator+();

    COperator &operator++();

    COperator operator++(int);//int是一个标志,告诉编译器这是一个后置++

    //注意后置++并不是在原有对象上直接+1,首次返回的是原始值,下次才是++的值,所以不要加引用,注意与前置++的区别

    //COperator &operator--();

    //COperator operator--(int);


    /*二元运算符*/

    COperator &operator+(const COperator &c);//减运算类同

    

public:

    int m_value;

};


#endif /* COperator_hpp */


//  COperator.cpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/18.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//


#include "COperator.hpp"


COperator::COperator(){}


COperator::~COperator(){}


COperator &COperator::operator-()

{

    m_value = -m_value;

    return *this;

}


COperator &COperator::operator+()

{

    m_value = +m_value;

    return *this;

}


COperator &COperator::operator++()

{

    m_value = ++m_value;

    return *this;

}


COperator COperator::operator++(int)

{

    COperator old(*this); //使用默认拷贝构造函数谨防浅拷贝

    m_value++;

    return old; //返回原来的值,下次调用才是++后的值

}


//COperator &COperator::operator--()

//{

//    --m_value;

//    return *this;

//}

//

//COperator COperator::operator--(int)

//{

//    COperator old(*this);

//    m_value--;

//    return old;

//}


COperator &COperator::operator+(const COperator &c)

{

    COperator outObj;

    outObj.m_value = this->m_value + c.m_value;

    return outObj;

}


COperator &operator--(COperator &c)

{

    c.m_value = --c.m_value;

    return c;

}


COperator operator--(COperator &c, int)

{

    COperator old(c);

    c.m_value = --c.m_value;

    return old;

}


COperator &operator-(const COperator c1, const COperator c2)

{

    COperator c3;

    c3.m_value = c1.m_value - c2.m_value;

    return c3;

}


std::ostream &operator<<(std::ostream &out, COperator &c)

{

    out<<c.m_value;

    return out;

}



//  main.cpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//


#include <iostream>

#include "COperator.hpp"

using namespace std;



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

    // insert code here...

    COperator coperator;

    coperator.m_value = 9;

    -coperator;

    cout<<coperator.m_value<<endl;

    -coperator;

    cout<<(coperator++).m_value<<endl;

    cout<<coperator.m_value<<endl;

    //cout<<(coperator--).m_value<<endl;

    cout<<coperator.m_value<<endl;

    cout<<(++coperator).m_value<<endl;

    //cout<<(--coperator).m_value<<endl;

    cout<<(--coperator).m_value<<endl;

    cout<<(coperator--).m_value<<endl;

    cout<<coperator.m_value<<endl;

    

    COperator c1;

    COperator c2;

    c1.m_value = 1;

    c2.m_value = 3;

    COperator c3;

    c3 = c1 + c2;

    cout<<c3.m_value<<endl;

    c3 =c2 - c1;

    cout<<c3.m_value<<endl;

    cout<<c3<<endl;;

    

    return 0;

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值