Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) In a header file, create an Account class that a bank might use to represent

ID: 3802227 • Letter: A

Question

a) In a header file, create an Account class that a bank might use to represent customers' bank accounts. b) Include a private data member called balance (of type int) to represent the account balance. c) Provide a constructor that has a parameter and use it to initialize the data member. d) Provide a public member function getBalance, which should return the balance. e) Provide a public member function setBalance, that has a parameter and assigns the parameter to the data member f) Write a complete program that will have a main function. Assume main function and class definition are in separate files. In main, create an object of class Account and initialize the balance to 500. g) From main, change the balance of same Account object to 1000. h) From main, print balance of same Account object.

Explanation / Answer

Create a header file Account.h with following contents

class Account
{
private:
       int balance;
      
      
   public:
       Account(int initBalance){
       balance = initBalance;
       }
      
       int getBalance(void)
       {
           return balance;
       }

       void setBalance(int newBalance)
       {
           balance = newBalance;
       }
};

Now create an another file Account.cpp with following contents which includes the above header file.

#include<iostream>
#include "Account.h"
using namespace std;


int main()
{
Account acc1(500);

acc1.setBalance(1000);

cout<< "Account balance : " << acc1.getBalance() <<endl;

return 0;
}