2 SavingsAccount class CHANGE TO A TEMPLATE CLASS . Add accountNumber to the con
ID: 3915754 • Letter: 2
Question
2 SavingsAccount class
CHANGE TO A TEMPLATE CLASS . Add accountNumber to the constructor. You may make interestRate ‘protected’ so you can directly access it in CDAccount. No other modifications.
//
// savingaccount.cpp
// Homework 3
//
// Created by ines said on 6/20/18.
// Copyright © 2018 ines said. All rights reserved.
//
#include "savingaccount.h"
double SavingAccount::calcInterest() // defining the calcInterest function
{
return getBalance() * interestRate ; // returning the balance after interest rate
}
//
// savingaccount.hpp
// Homework 3
//
// Created by ines said on 6/20/18.
// Copyright © 2018 ines said. All rights reserved.
//
#ifndef savingaccount_h
#define savingaccount_h
#include<iostream>
#include "account.h"
using namespace std;
class SavingAccount:public account
{
private: // declaring private members
double interestRate;
public: // declaring public members
// default constrcutor
SavingAccount() : account(){
interestRate = 0; // sets interest rate to 0
}
// parametrized constrcutor
SavingAccount(double bal, double interest) : account(bal){
if (interestRate > 0 ) // check if the given interest rate is more than 0
{
interestRate = interest; // if it is more than 0 assign it to ineterest rate
}
else // if interest rate is not more than 0 prints error and set it to 0
{ cout << "Error interest Rate less than 0" << endl;
interestRate = 0;}
}
// destructor
~SavingAccount(){
}
double calcInterest(); // fucntion to calculate interest
};
#endif /* savingaccount_hpp */
Explanation / Answer
when you convert the class in to a template class it will look like this.
//
// savingaccount.cpp
// Homework 3
//
// Created by ines said on 6/20/18.
// Copyright © 2018 ines said. All rights reserved.
//
#include "savingaccount.h"
template<typename T>
double SavingAccount<T>::calcInterest() // defining the calcInterest function
{
return getBalance() * interestRate ; // returning the balance after interest rate
}
//
// savingaccount.hpp
// Homework 3
//
// Created by ines said on 6/20/18.
// Copyright © 2018 ines said. All rights reserved.
//
#ifndef savingaccount_h
#define savingaccount_h
#include<iostream>
#include "account.h"
using namespace std;
template <typename T>
class SavingAccount:public account
{
private: // declaring private members
int accountNumber;
protected:
T interestRate;
public: // declaring public members
// default constrcutor
SavingAccount() : account(){
accountNumber=1234;
interestRate = 0; // sets interest rate to 0
}
// parametrized constrcutor
SavingAccount(T bal, T interest) : account(bal){
accountNumber=1234;
if (interestRate > 0 ) // check if the given interest rate is more than 0
{
interestRate = interest; // if it is more than 0 assign it to ineterest rate
}
else // if interest rate is not more than 0 prints error and set it to 0
{ cout << "Error interest Rate less than 0" << endl;
interestRate = 0;}
}
// destructor
~SavingAccount(){
}
double calcInterest(); // fucntion to calculate interest
};
#endif /* savingaccount_hpp */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.