码迷,mamicode.com
首页 > 编程语言 > 详细

C++重载运算符

时间:2016-05-24 20:56:27      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
using namespace std;
class Byte{
    public:
        Byte(){b=0;}
        Byte(int k){b=k;}
    const Byte operator+(const Byte& right)const{
        return Byte(b+right.b);
    }
    const Byte operator-(const Byte& right)const{
        return Byte(b-right.b);
    }
    const Byte operator*(const Byte& right)const{
        return Byte(b*right.b);
    }

    Byte& operator+=(const Byte& right){
        b+=right.b;
        return *this; 
    }
    
    bool operator ==(const Byte& right)const{
        return b==right.b;    
    }
    int getnum(){
        return b;
    }
    private:
        int b;
};
int main(){
    Byte test(3),k(3),haha;
    if(test==k)
    cout<<"true"<<endl;

Byte test(3),k(2),haha;
test+=k;
cout<<test.getnum()<endl;

return 0;
}

 

C++重载运算符

标签:

原文地址:http://www.cnblogs.com/test404/p/5524509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!