c++继承

#include <iostream>

using namespace std;
typedef long long LL;

class RMB{
    LL a,b,c;
    static int count;
public:
    RMB():a(0),b(0),c(0){
        count++;
        cout<<count<<endl;
    };
    RMB(LL a,LL b,LL c):a(a),b(b),c(c){
        count++;
        cout<<count<<endl;
    };
    ~RMB(){
        count--;
        cout<<"count="<<count<<endl;
    };
    LL convert()const{
        return this->a*100+this->b*10+this->c;
    }
    void convertToRMB(LL money){
        this->a=money/100;
        this->b=money/10%10;
        this->c=money%10;
    }
    bool operator>(const RMB &other)const{
        return this->convert()>other.convert();
    }
    RMB operator+(const RMB &rmb)const{
        RMB temp;
        temp.convertToRMB(this->convert()+rmb.convert());
        return temp;
    }
    RMB operator-(const RMB &rmb)const{
        RMB temp;
        temp.convertToRMB(this->convert()-rmb.convert());
        return temp;
    }
    RMB &operator--(){
        this->convertToRMB(this->convert()-111);
        return *this;
    }
    RMB operator--(int){
//        RMB rmb=*this;//这里调用默认的拷贝构造函数,导致count不会增加,但是会调用析构函数,导致count多减一次;
        RMB rmb;
        rmb.convertToRMB(this->convert());
        this->convertToRMB(this->convert()-111);
        return rmb;
    }
    void show(){
        cout<<"the object count="<<count<<endl;
        cout<<"object[yuan,jiao,fen]=";
        cout<<"["<<"a="<<a<<",b="<<b<<",c="<<c<<"]"<<endl;
    }
};
int RMB::count=0;
int main()
{
    RMB a;
    a.show();
    cout<<"========="<<endl;
    RMB b(100,99,9);
    b.show();
    cout<<"=========="<<endl;
    cout<<"+重载"<<endl;
    a=a+b;
    a.show();
    cout<<"=========="<<endl;
    cout<<"-重载"<<endl;
    a=a-b;
    a.show();
    cout<<"=========="<<endl;
    cout<<"前--重载"<<endl;
    --b;
    b.show();
    cout<<"后--重载"<<endl;
    b--;
    b.show();
    cout<<"==========="<<endl;
    return 0;
}

1
the object count=1
object[yuan,jiao,fen]=[a=0,b=0,c=0]
=========
2
the object count=2
object[yuan,jiao,fen]=[a=100,b=99,c=9]
==========
+重载
3
count=2
the object count=2
object[yuan,jiao,fen]=[a=109,b=9,c=9]
==========
-重载
3
count=2
the object count=2
object[yuan,jiao,fen]=[a=0,b=0,c=0]
==========
前--重载
the object count=2
object[yuan,jiao,fen]=[a=108,b=8,c=8]
后--重载
3
count=2
the object count=2
object[yuan,jiao,fen]=[a=107,b=7,c=7]
===========
count=1
count=0

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值