Demonstrate the ability to create and manipulate classes, data members, and memb
ID: 3533794 • Letter: D
Question
Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time.
Create a C++ project to do implement a simplified banking system.
Step 1:
Create a header file Bank.h to define a Bank class with the following members:
Data members: The program should implement data hiding, so please define data members accordingly.
- accountNumber
- balance
Note: The data types are not specified for this problem, so as to give you flexibility to design the program in your way.
Member functions: Please include only the declaration of the member functions in the header file. The implementation of member functions will later be done in the Bank.cpp file.
- Bank() constructor with no arguments to create a bank account with a default accountNumber as 9999 and balance value of zero.
- Bank(param1, param2) constructor with two parameters to create a bank account with a specified accountNumber and balance (as param1 and param2, respectively).
- withdraw(param1) function to withdraw a specified amount (param1) from the account. The function should first check if there is sufficient balance in the account. If the balance is sufficient, withdrawal is processed, otherwise display a message to the user that says
Explanation / Answer
- Bank.h
- class Bank
- {
- private:
- int accountNumber;
- float balance;
- public:
- Bank();
- Bank(int num, float bal);
- void withdraw(float sum);
- void deposit(float sum);
- int getAccountNumber();
- float getBalance();
- };
- Bank.cpp
- #include "Bank.h"
- #include <iostream>
- using namespace std;
- Bank::Bank ()
- {
- accountNumber = 9999;
- balance = 0.0;
- }
- Bank::Bank (int num, float bal)
- {
- accountNumber = num;
- balance = bal;
- }
- void Bank::withdraw (float sum)
- {
- if (balance >= sum)
- {
- balance -= sum;
- }
- else
- {
- std::cout << "Insufficient funds" << std::endl;
- balance = 0;
- }
- }
- void Bank::deposit (float sum)
- {
- balance += sum;
- }
- int Bank::getAccountNumber ()
- {
- return accountNumber;
- }
- float Bank::getBalance ()
- {
- return balance;
- }
- void displayAccountInfo (Bank acc)
- {
- cout << "Account Number: " << acc.getAccountNumber();
- cout << endl;
- cout << "Balance: $" << acc.getBalance();
- cout << endl;
- }
- int main()
- {
- Bank accnt1;
- accnt1.withdraw(500.00);
- Bank accnt2(1111, 1000.00);
- accnt2.withdraw(600.00);
- accnt2.withdraw(300.00);
- accnt2.deposit(400.00);
- displayAccountInfo(accnt1);
- displayAccountInfo(accnt2);
- }
- Bank.h
- class Bank
- {
- private:
- int accountNumber;
- float balance;
- public:
- Bank();
- Bank(int num, float bal);
- void withdraw(float sum);
- void deposit(float sum);
- int getAccountNumber();
- float getBalance();
- };
- Bank.cpp
- #include "Bank.h"
- #include <iostream>
- using namespace std;
- Bank::Bank ()
- {
- accountNumber = 9999;
- balance = 0.0;
- }
- Bank::Bank (int num, float bal)
- {
- accountNumber = num;
- balance = bal;
- }
- void Bank::withdraw (float sum)
- {
- if (balance >= sum)
- {
- balance -= sum;
- }
- else
- {
- std::cout << "Insufficient funds" << std::endl;
- balance = 0;
- }
- }
- void Bank::deposit (float sum)
- {
- balance += sum;
- }
- int Bank::getAccountNumber ()
- {
- return accountNumber;
- }
- float Bank::getBalance ()
- {
- return balance;
- }
- void displayAccountInfo (Bank acc)
- {
- cout << "Account Number: " << acc.getAccountNumber();
- cout << endl;
- cout << "Balance: $" << acc.getBalance();
- cout << endl;
- }
- int main()
- {
- Bank accnt1;
- accnt1.withdraw(500.00);
- Bank accnt2(1111, 1000.00);
- accnt2.withdraw(600.00);
- accnt2.withdraw(300.00);
- accnt2.deposit(400.00);
- displayAccountInfo(accnt1);
- displayAccountInfo(accnt2);
- }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.