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;
}