Hamdard University Islamabad Campus: Lab No

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Page 1 of 4

Hamdard University Islamabad Campus


Hamdard Institute of Engineering & Technology
Department of Computing

Program: BS Computer Science

Course Number CS122


Course Title Object Oriented Programming
Semester/Year Fall 2020

Instructor Ms. Saadia Mooqaddas

LAB NO. 06

Assignment Title Lab NO 6

Submission Date
Due Date

Student Name Muhammad Umer Farooq


Student CMS-ID 445-2020
Signature*

*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

void SavingsAccount::modifyIntererestRate(float value)


{
annualInterestRate = value;
}

int main()
{

SavingsAccount saver1(2000.00);
SavingsAccount saver2(3000.00);

SavingsAccount::modifyIntererestRate(3);// annual interset rate 3%

saver1.calculateMonthlyInterest();
cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << endl;
saver2.calculateMonthlyInterest();
cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << endl;

cout << 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;

cout << endl;


return 0;
}
Page 4 of 4

Program Output:

You might also like