Professional Documents
Culture Documents
Hamdard University Islamabad Campus: Lab No
Hamdard University Islamabad Campus: Lab No
Hamdard University Islamabad Campus: Lab No
LAB NO. 06
Submission Date
Due Date
*By signing above, you attest that you have contributed to this submission and
confirm that all work you have contributed to this submission is your own work. Any
suspicion of copying or plagiarism in this work will result in an investigation of
Academic Misconduct and may result in a “0” on the work, an “F” in the course, or
possibly more severe penalties.
Page 2 of 4
Lab Tasks
Task # 1
Program Statement:
Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual
interest rate for each of the savers. Each member of the class contains a private data member
savingsBalance indicating the amount the saver currently has on deposit. Provide member function
calculateMonthlyInterest that calculates the monthly interest by multiplying the savingsbalance by
annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static
member function modifyInterestRate that sets the static annualInterestRate to a new value. Write a
driver program (main program) to test class SavingsAccount. Instantiate two different objects of
class SavingsAccount, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively.
Set the annualInterestRate to 3 percent. Then calculate the monthly interest and print the new
balances for each of the savers. Then set the annualInterestRate to 7 percent, calculate the next
month's interest and print the new balances for each of the savers.
Program Code:
#include <iostream>
using namespace std;
class SavingsAccount
{
public:
SavingsAccount(){}
~SavingsAccount(){}
SavingsAccount(int value);
static float annualInterestRate;
void calculateMonthlyInterest();
static void modifyIntererestRate(float value);
float GetBalance() const
{
return savingsBalance;
}
private:
float savingsBalance;
};
SavingsAccount::SavingsAccount(int value)
{
savingsBalance = value;
}
//
float SavingsAccount::annualInterestRate = 0;
void SavingsAccount::calculateMonthlyInterest()
{
savingsBalance += ((savingsBalance * annualInterestRate) / 12);
Page 3 of 4
int main()
{
SavingsAccount saver1(2000.00);
SavingsAccount saver2(3000.00);
saver1.calculateMonthlyInterest();
cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << endl;
saver2.calculateMonthlyInterest();
cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << endl;
SavingsAccount::modifyIntererestRate(7);// 7%
saver1.calculateMonthlyInterest();
cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << endl;
saver2.calculateMonthlyInterest();
cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << endl;
Program Output: