c++静态成员函数,继承

#include <iostream>

using namespace std;

class RMB
{
private:
    int Y;//元
    int J;//角
    int F;//分
    static int count;//成员个数
public:
    //无参构造函数
    RMB()
    {
        count++;
    }
    //有参构造函数
    RMB(int Y,int J,int F):Y(Y),J(J),F(F)
    {
        count++;
    }
    void show()
    {
        if(F >= 10)
        {
            J += F / 10;
            F %= 10;
        }
        if(J >= 10)
        {
            Y += J / 10;
            J %= 10;
        }
        double rmb = Y + J/10.0 + F/100.0;
        cout << "RMB:" << rmb << "元" << endl;
    }
    //析构函数
    ~RMB()
    {
        count--;
    }
    //获取count
    static int getcount()
    {
        return count;
    }
    //成员函数实现+号运算符重载
    const RMB operator+(const RMB &R) const
    {
        RMB temp;
        temp.Y = Y + R.Y;
        temp.J = J + R.J;
        temp.F = F + R.F;
        return temp;
    }
    //成员函数实现-号运算符重载
    const RMB operator-(const RMB &R) const
    {
        RMB temp;
        temp.Y = Y - R.Y;
        temp.J = J - R.J;
        temp.F = F - R.F;
        return temp;
    }
    //成员函数实现>号运算符重载
    bool operator>(const RMB &R) const
    {
        if((Y+J/10.0+F/100.0)>(R.Y+R.J/10.0+R.F/100.0))
            return true;
        else
            return false;
    }
    //成员函数实现前--运算符重载
    RMB &operator--()
    {
        --Y;
        --J;
        --F;
        return *this;
    }
    //成员函数实现后--运算符重载
    const RMB operator--(int)
    {
        RMB temp;
        temp.Y = Y--;
        temp.J = J--;
        temp.F = F--;
        return temp;
    }
};
int RMB::count = 0;

int main()
{
    RMB a(20,20,20);
    RMB b(5,5,5);

    RMB c = a + b;
    c--;
    c.show();
    RMB d = a - b;
    --d;
    d.show();
    RMB *p = new RMB;
    if(c>d)
    {
        cout << "c>d" << endl;
    }
    cout << "目前已创建RMB对象的数量:" << RMB::getcount() << endl;
    delete p;
    cout << "目前已创建RMB对象的数量:" << RMB::getcount() << endl;

    return 0;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值