For this lab you must write a complete class in C++. This will be a class named
ID: 653021 • Letter: F
Question
For this lab you must write a complete class in C++. This will be a class named BankAccount. This class must have the following private variables:
1. accountHolderName : string
2. balance : double
3. interestRate: double
This class must have the following public constructor:
1. BancAccount(name : string, balance : double, rate : double)
This class must have the following public member functions:
1. getAccountHolderName() : string a. returns the account holders name
2. getBalance() : double a. returns the current balance
3. getInterestRate() : double a. returns the current interest rate
4. deposit(amount : double) : void a. adds the amount to the balance
5. withdraw(amount : double) : void a. subtracts the amount from the balance
6. applyInterest() : void a. adds interest (balance * interestRate) to the balance
You must write the complete and correct BankAccount.h file that will contain the class declaration.
You must write the complete and correct BankAccount.cpp file that will contain the implementation code.
Explanation / Answer
bankaccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
class BankAccount {
private:
std::string accountHolderName ;
double balance;
double interestRate; // in dollars
public:
BankAccount(std::string name, double balance, double rate);
std::string getAccountHolderName();
double getBalance();
double getInterestRate();
void deposit(double amount);
void withdraw(double amount);
} ;
#endif
BankAccount.cpp
#include "bankaccount.h"
#include<iostream>
#include <string.h>
using namespace std;
double balance;
double interestRate;
BankAccount::BankAccount(std::string initaccountHolderName, double initbalance,
double initrate) {
balance = initbalance;
accountHolderName = initaccountHolderName;
interestRate = initrate;
}
double BankAccount::getBalance () {
return balance;
}
string BankAccount::getAccountHolderName () {
return accountHolderName;
}
void BankAccount::deposit (double amount) {
balance += amount;
}
double BankAccount::getInterestRate () {
return interestRate;
}
void BankAccount::withdraw (double amount) {
if (balance < amount)
cerr << "Insufficient funds ";
else
balance -= amount;
}
void applyInterest(){
balance = balance + balance*interestRate;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.