Change to a template class and add/modify the following items. 1.1 Data Members
ID: 3914657 • Letter: C
Question
Change to a template class and add/modify the following items.
1.1 Data Members
accountNumber (string)
1.2 Member Functions Constructors
Add accountNumber to the constructor.
Accessors
string getAccountNumber() Returns the account number.
void displayAccount() Prints the account type, fee or interest rate, and balance. Determine if this should be virtual, pure virtual, or regular.
//
// account.cpp
// Homework 3
//
// Created by ines said on 6/20/18.
// Copyright © 2018 ines said. All rights reserved.
//
#include "account.h"
#include <iostream>
using namespace std;
account::account() // Default constrcutor
{
cout << "Constructor called." << endl;
// balance = new double;
balance = 0.0; } // default balance
account::account(double bal)
{
if (bal < 0) // checks if balance is below 0
{ balance = 0;
cout << "Error: Balance Bellow Zero" << endl; //prints output sentence on screen
}
else balance = bal;
}
// destructor function
account::~account() {
cout << "Destructor called." << endl; // print message to show the call of teh destrcutor
delete this ;
}
double account::getBalance()
{
return balance;
}
void account::debit(double amount)
{
if (amount <= balance ) //check that the amount does not exceed the balance
{
balance = balance - amount; // withdraws the amount from the balance
}
else cout << "Error !! amount exceed balance" << endl; // pritn error messafe if teh amount exceeds the balance
}
void account::credit(double amount)
{
if (amount > 0 ) // check if the amount is more than 0
balance = balance + amount ; // deposits the amount
}
//
// account.hpp
// Homework 3
//
// Created by ines said on 6/20/18.
// Copyright © 2018 ines said. All rights reserved.
//
#ifndef account_hpp
#define account_hpp
#include <stdio.h>
using namespace std;
class account
{
private: // private class members
double balance;
public: // oublic class members
account();
account(double bal);
double getBalance();
virtual void debit(double amount); // declare a virtual function to add the amount
virtual void credit(double amount); // declare a virtual function to withdraw the amount
~account(); // declaring destructor
};
#endif /* account_hpp */
Explanation / Answer
/* account.h */
#ifndef account_hpp
#define account_hpp
#include <stdio.h>
using namespace std;
template <class A, class B>
class account
{
private: // private class members
A balance,interestRate;
B accountNumber,accountType;
public: // oublic class members
account();
account(A bal);
B getAccountNumber();
A getBalance();
void displayAccount();
virtual void debit(A amount); // declare a virtual function to add the amount
virtual void credit(A amount); // declare a virtual function to withdraw the amount
~account(); // declaring destructor
};
#endif /* account_hpp */
/* account.cpp */
#include "account.h"
#include <iostream>
using namespace std;
template <class A, class B>
account<A,B>::account() // Default constrcutor
{
cout << "Constructor called." << endl;
// balance = new double;
balance = 0.0; // Default balance is 0
accountNumber = "0"; // Default account number is 0
accountType = "Savings"; // Default account Type is SAVINGS
interestRate = 4.5; // Default Rate of interest is 4.5% p.a.
} // default balance
template <class A, class B>
account<A,B>::account(A bal)
{
if (bal < 0) // checks if balance is below 0
{ balance = 0;
cout << "Error: Balance Bellow Zero" << endl; //prints output sentence on screen
}
else balance = bal;
}
// destructor function
template <class A, class B>
account<A,B>::~account() {
cout << "Destructor called." << endl; // print message to show the call of teh destrcutor
delete this ;
}
// getAccountNumber Function returns account number
template <class A, class B>
B account<A,B>::getAccountNumber()
{
return accountNumber; //Returns account number
}
template <class A, class B>
A account<A,B>::getBalance()
{
return balance;
}
template <class A, class B>
void account<A,B>::displayAccount()
{
cout<<"Account type: "<<accountType;
cout<<" Interest Rate: "<<interestRate;
cout<<" Account Balance: "<<getBalance();
}
template <class A, class B>
void account<A,B>::debit(A amount)
{
if (amount <= balance ) //check that the amount does not exceed the balance
{
balance = balance - amount; // withdraws the amount from the balance
}
else cout << "Error !! amount exceed balance" << endl; // pritn error messafe if teh amount exceeds the balance
}
template <class A, class B>
void account<A,B>::credit(A amount)
{
if (amount > 0 ) // check if the amount is more than 0
balance = balance + amount ; // deposits the amount
}
/*END OF account.cpp*/
/* Example of main() function below shows how to use account class as template class
int main()
{
account <double, string> a(54.6);
return 0;
}
*/
/* In template class, placeholder A will be replaced by double and placeholder B will be replaced by string*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.