C++ Classes Lab Exercise: This portion of the lab should be completed during you
ID: 3672173 • Letter: C
Question
C++
Classes
Lab Exercise: This portion of the lab should be completed during your lab session.
The exercise for this week is to write a class that simulates managing a simple bank account. The account is
created with an initial balance. It is possible to deposit and withdraw funds, to add interest, and to find out the
current balance. This should be implemented in class named Account that includes:
• A default constructor that sets the initial balance to zero.
• A constructor that accepts the initial balance as a parameter.
• A function getBalance that returns the current balance.
• A method deposit for depositing a specified amount.
• A method withdraw for withdrawing a specified amount.
• A method addInterest for adding interest to the account.
The addInterest method takes the interest rate as a parameter and changes the balance in the account to
balance*(1+interestRate).
The UML diagram for the Account class is shown in figure 1.
-balance: double
+Account();
+Account(double);
+double getBalance();
+void deposit(double );
+void withdraw(double);
+void addInterest(double);
Account
Figure 1: The Account Class
Your class must work with the code given below and display the output .
240
450
Lab7.cpp (main)
#include
using namespace std;
#include "Account.h"
int main()
{
Account a1;
Account a2(500);
a1.depost(200);
a2.withdraw(50);
a1.addInterest(0.2);
cout< cout<<" ";
cout< system("pause");
return 0;
}
Explanation / Answer
//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
//Account class defintion
class Account
{
private:
//declare a balance variable
double balance;
public:
//Account constuctors
Account();
Account(double);
//method declarations
double getBalance();
void deposit(double );
void withdraw(double);
void addInterest(double);
};
#endif ACCOUNT_H
------------------- ------------------- ------------------- -------------------
//Account.cpp
//Implementaiton file of Account.h
//include header file Account.h
#include "Account.h"
//default account constructor to set balance to zero
Account::Account()
{
//set balance =0
balance=0;
}
//Set input argumet bal to balance
Account::Account(double bal)
{
balance=bal;
}
//Returns the balance amount
double Account::getBalance()
{
return balance;
}
//Desposit amount to balance
void Account::deposit(double amount)
{
balance=balance+amount;
}
//Withdraws amount from balance
void Account::withdraw(double amount)
{
balance=balance-amount;
}
//Find the interest value and add to the balance
void Account::addInterest(double interestRate)
{
balance=balance*(1+interestRate);
}
------------------- ------------------- ------------------- -------------------
/**The c++ tester program that tests the Account class object
and calls the deposit and withdraw methods and print the account
balance*/
//tester.cpp
#include<iostream>
//include Account header file
#include "Account.h"
using namespace std;
int main()
{
//create an object of Account class
Account a1;
//create an object of Account class with 500 initial balance
Account a2(500);
//call deposit method to deposit 200 from a1 object
a1.deposit(200);
//call withdraw method to withdraw 50 from a2 object
a2.withdraw(50);
//call addInterest 20 % (0.2) to a1
a1.addInterest(0.2);
//print a1 account balance
cout<<"a1-account"<<endl;
cout<<"Balance :"<<a1.getBalance()<<endl;
cout<<"a2-account"<<endl;
//print a2 account balance
cout<<"Balance :"<<a2.getBalance()<<endl;
//pause the program output on console
system("pause");
return 0;
}
------------------- ------------------- ------------------- -------------------
SampleOuput :
a1-account
Balance :240
a2-account
Balance :450
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.