I have a homework question. My problem is...
Develop a polymorphic banking program using the Account hierarchy created in Exercise 12.10. Create a vector of Account pointers to SavingsAccount and CheckingAccount objects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the Account using member function calculateInterest, then add the interest to the account balance using member function credit. After processing an Account, print the updated account balance obtained by invoking base class member function getBalance
I have created classes and their member functions.
Code:
#include <iostream>
using namespace std;
class Account{
public:
Account( double );
void credit( double );
void debt( double );
double getBalance();
private:
double accountBalance;
};
class SavingsAccount: public Account{
public:
SavingsAccount( double, double );
double calculateInterest();
private:
double interestRate;
};
class CheckingAccount: public Account{
public:
CheckingAccount( double, double );
void credit( double );
void debt( double );
private:
double fee;
};
Account::Account( double initialbalance ){
if( initialbalance < 0 ){
accountBalance = 0;
cout << "Invalid entry, account balance is $0.00." << endl;
}
else
accountBalance = initialbalance;
}
void Account::credit( double creditamount ){
if( creditamount < 0 )
cout << "Credit amount is less than $0.00, account has not been credited." << endl;
else
accountBalance += creditamount;
}
void Account::debt( double debtamount ){
if( ( accountBalance - debtamount ) < 0 )
cout << "Debt amount exceed account balance, account has not been debited." << endl;
else
accountBalance -= debtamount;
}
double Account::getBalance(){
return accountBalance;
}
SavingsAccount::SavingsAccount( double initialbalance, double interest ) : Account( initialbalance ){
if( interest < 0 ){
interestRate = 0;
cout << "Invalid entry, account interest rate is 0%." << endl;
}
else
interestRate = interest;
}
double SavingsAccount::calculateInterest(){
return getBalance() * ( interestRate / 100 );
}
CheckingAccount::CheckingAccount( double initialbalance, double transfee ) : Account( initialbalance ){
if( transfee < 0 ){
fee = 0;
cout << "Invalid entry, account transaction fee is $0.00." << endl;
}
else
fee = transfee;
}
void CheckingAccount::credit( double deposit ){
if( ( getBalance() + deposit - fee ) < 0 )// Makes sure fee doesn't drop account balance below $0.00
cout << "Transaction would bring account below $0.00. Transaction not performed." << endl;
else{
Account::credit( deposit ); // Uses Account credit() to credit account
Account::debt( fee );
}
}
void CheckingAccount::debt( double withdrawl ){
if( ( getBalance() - withdrawl - fee ) < 0 )// Makes sure fee doesn't drop account balance below $0.00
cout << "Transaction would bring account below $0.00. Transaction not performed." << endl;
else
Account::debt( withdrawl + fee ); // Uses Account debt() to debit account
}
I can create the array of pointers and manipulate the member functions of the derived class by doing this....
Code:
for(int i=0; i<8; i++){
if(i % 2 == 0)
accounts[i] = new SavingsAccount(0, 2);
else
accounts[i] = new CheckingAccount(0, 3);
}
accounts[0]->credit(200);
accounts[0]->debt(100);
cout << accounts[0]->getBalance();
for(int i=0; i<8; i++)
delete accounts[i];
What I don't know how to do it determine the class type and how to manipulate the calculateInterest function. How do I determine the type of the derived class and how do I manipulate the member functions of the derived classes?