银行账户问题

Suppose there is a class PersonalAccount, which has a private member variable balance (type: integer and value is 0 at first);Besides, this class has three member functions:
1. add, add money into user’s account.
2. subtract, subtract money from user’s account, if the balance is insufficient, do not subtract money and print message “insufficient balance..”
3. getBalance, print the user’s balance like “The Balance is: xx”.

/* from younglee..
 * guys! enjoy your journey of c plus plus, against all odds!
 */
#include<iostream>
using namespace std;

class PersonalAccount {
    private:
        int balance;
    public:
        PersonalAccount();
        void add(int value);
        void subtract(int value);
        void getBalance();
};

int main() {
    // test input here..
    int a, b;
    cin >> a >> b;

    // declare a object and it's balance is 0.
    PersonalAccount pa;

    // add a dollar into user's account..
    pa.add(a);

    // deduct b dollar from user's account..
    pa.subtract(b);

    // show balance..
    pa.getBalance();
}

PersonalAccount::PersonalAccount () {
    balance = 0;
}

// add money into user's account..
void PersonalAccount::add(int value) {
    balance += value;
}

// deduct money from user's account..
void PersonalAccount::subtract(int value) {
    if (balance - value >= 0) {
        balance -= value;
    } else {
        cout << "insufficient balance.." << endl;
    }
}

// get the account's balance..
void PersonalAccount::getBalance() {
    cout << "The Balance is: " << balance << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值