Account's Balance(for lab)

来源:互联网 发布:开网店用什么软件 编辑:程序博客网 时间:2024/05/18 03:34

Account’s Balance(for lab)

标签(空格分隔): 程序设计实验 c++

本人学院

  • Accounts Balancefor lab
    • 标签空格分隔 程序设计实验 c
    • 本人学院
    • Description
    • 读题
    • my answer
    • the standard answer


Description:

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”.

The main.cpp and .h are given, please finish the class’s definition..
main.cpp

/* from younglee.. * guys! enjoy your journey of c plus plus, against all odds! */#include<iostream>#include "PersonalAccount.h"using namespace std;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.h

#ifndef PERSONALACCOUNT_H#define PERSONALACCOUNT_Hclass PersonalAccount {    private:        int balance;    public:        PersonalAccount();        void add(int value);        void subtract(int value);        void getBalance();};#endif

读题

my answer

#include<iostream>#include"PersonalAccount.h"using namespace std;void PersonalAccount::add(int value) {    balance += value;}void PersonalAccount::subtract(int value) {    if (balance < value) {        cout << "insufficient balance.." << endl;    } else {        balance -= value;    }}void PersonalAccount::getBalance() {    cout << "The Balance is: " << balance << endl;}PersonalAccount::PersonalAccount() {    balance = 0;}

the standard answer

#include<iostream>#include"PersonalAccount.h"using namespace std;// the constructor..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;}// the end..
0 0