main.cpp
#include <iostream> #include "BankAccount.h" using namespace std; int main() { BankAccount bankAccount1(200), bankAccount2, bankAccount3(-100); cout << BankAccount::getAllMoneyInBank() << endl; bankAccount1.withdraw(100); cout << bankAccount1.getBalance() << endl; cout << BankAccount::getAllMoneyInBank() << endl; bankAccount2.save(50); cout << bankAccount2.getBalance() << endl; cout << BankAccount::getAllMoneyInBank() << endl; return 0; }
BankAccount.h
#pragma once class BankAccount { private: int balance; static int total; public: BankAccount() { balance = 0; } BankAccount(int input) { balance = input; total += input; } void withdraw(int output); void save(int input); int getBalance() { return balance; } static int getAllMoneyInBank() { return total; } };
BankAccount.cpp
#include"BankAccount.h" int BankAccount::total = 0; void BankAccount::withdraw(int output) { balance -= output; total -= output; } void BankAccount::save(int input) { balance += input; total += input; }
沒有留言:
張貼留言