For C++ Visual Studios: Class Inheritance hierarchy. A console based interface B
ID: 3713106 • Letter: F
Question
For C++ Visual Studios:
Class Inheritance hierarchy. A console based interface Build your classes each in their own .h and .cpp files, then test them with the simple main method provided below.
Account
The generic BASE class Account will serve as the parent class to Checking, Savings, and CreditCard.
Variables (private):
name – Name of account owner, a string
taxID – social security number, a long
balance – an amount in dollars, a double
Variables (protected):
last10withdraws – a double array of size 10. The last 10 withdrawal amounts.
last10deposits – a double array of size 10. The last 10 deposit amounts.
numdeposits – number of deposits, an int
numwithdraws – number of withdrawals, an int
Methods:
SetName, SetTaxID, Setbalance() assigns a new value for each with error checking
GetName, GetTaxID, Getbalance() returns a value for each variable.
MakeDeposit( double amount ) - adjust the balance and put it in the deposit array
A constructor with no parameters and one with name, taxID and balance parameters
display() a method to display the name, taxID and balance
Checking
A specific DERIVED class that represents a bank checking account. It must inherit Account.
Variables (private):
last10checks – an int array of size 10. The last 10 check numbers.
Methods:
WriteCheck( int checknum, double amount ) - adjust the balance and list it as a withdraw in the base class
A constructor with no parameters and one with name, taxID and balance parameters
display() - display the accounts check register (number and amount) and deposit record
Savings
A specific DERIVED class that represents a bank savings account. It must inherit Account.
Methods:
DoWithdraw( double amount) - adjust the balance and list it as a withdraw in the base class
A constructor with no parameters and one with name, taxID and balance parameters
display() - display the accounts withdrawal and deposit record
CreditCard
A specific DERIVED class that represents a credit card account. It must inherit Account.
Variables:
cardnumber – a long
last10charges – a string array of size 10. The last 10 names of the charges.
Methods:
DoCharge( string name, double amount ) - adjust the balance and list it as a withdraw in the base class
MakePayment( double amount) - adjust the balance and list it as a DEPOSIT in the base class
A constructor with no parameters and one with name, taxID and balance parameters
display() - display the accounts charges ( name and amount ) and deposit record
Note: all display() methods should use cout to output text to the console.
Write a main() Function
Write a main that creates 3 objects, starts each with a balance of $100. Create a loop that displays the following menu each time through the loop. Make sure the balance is updated each time AND you use the objects to perform the transactions.
Explanation / Answer
#ifndef BankAccount_H
#define BankAccount_H
#include<iostream>
using namespace std;
class BankAccount
{
public:
int accountNumber;
double balance;
//Constructor
BankAccount(int ano,double bal)
{
accountNumber=ano;
balance=bal;
}
BankAccount() { }
public:
//Accessor methods for getting balance
double getBalance()
{
return(balance);
}
//Accessor methods for getting account number
int getAccountNumber()
{
return(accountNumber);
}
//Mutuator method to set balance
void setBalance(double amount)
{
balance=amount;
}
//Setting account number
void setAccountNumber(int ano)
{
accountNumber=ano;
}
//show account information
void showAccountInfo()
{
cout<<"Account Number :"<<accountNumber<<endl;
cout<<"Account Balance:"<<balance<<endl;
}
//function to perform deposit
double depositAmount(double damount)
{
balance=balance+damount;
return(balance);
}
//withdrawl amount
double withDrawAmount(double wamount)
{
balance=balance-wamount;
return(balance);
}
};
#endif
#include <iomanip>
#include <iostream>
#include <cmath>
#ifndef SavingsAccount_H
#define SavingsAccount_h
using namespace std;
class SavingAccount: BankAccount
{
//data member
double minBal;
double balance;
double accountNumber;
public:
//Constructor
SavingAccount(int ano , double amount):BankAccount(ano, amount)
{
minBal=500;
}
//to perform deposit
//overriding base class function
double depositAmount(double damount)
{
balance=balance+damount;
return(balance);
}
//to perform withdraw
//overriding base class function
double withDrawAmount(double wamount)
{
if(check())
balance=balance-wamount;
else
cout<<"you have in sufficient balance"<<endl;
return(balance);
}
//Setting minimum balance
void setMinBal(double mbal)
{
minBal=mbal;
}
//Checking for account sufficiency
bool check()
{
if(balance<minBal)
return(false);
else
return(true);
}
//Showing account information
void showAccountInfo()
{
cout<<"Account Number :"<<accountNumber<<endl;
cout<<"Account Balance:"<<balance<<endl;
}
};
#endif
#include <iostream>
#include <iomanip>
#include <cmath>
#include "BankAccount.h"
#ifndef CheckingAccount_H
#define CheckingAccount_h
using namespace std;
class CheckingAccount : BankAccount
{
//Data members
double balance;
double interest;
double minBal;
double serviceCharge;
double accountNumber;
public://Constructore
CheckingAccount(int ano,double amount,double interest):BankAccount( ano, amount)
{
interest= interest;
minBal=500;//By default
}
//To set interest rate
void setInterestRate(double irate)
{
interest=irate;
}
//to get interest rate
double retrieveInterestRate()
{
return(interest);
}
//to set minimum balance
void setMinBal(double mbal)
{
minBal=mbal;
}
//To get minimum balance
double retrieveMinBal()
{
return(minBal);
}
//To set service charge
void setServiceCharge(double scharge)
{
serviceCharge=scharge;
}
//To get service charge
double retrieveServiceCharge()
{
return(serviceCharge);
}
//To perform deposit
//overriding base class function
double depositAmount(double damount)
{
balance=balance+damount;
return(balance);
}
//to perform withdraw
//overriding base class function
double withDrawAmount(double wamount)
{
//Checking for account have sufficient balance or not
if(check())
balance=balance-wamount;
else
cout<<"You have in sufficient balance";
return(balance);
}
//Check function for verifying balance
bool check()
{
if(balance<minBal)
return(false);
else
return(true);
}
//To display account information
void showAccountInfo()
{
cout<<"Account Number :"<<accountNumber<<endl;
cout<<"Account Balance:"<<balance<<endl;
}
};
#endif
#include <cmath>
#include <iomanip>
#include <iostream>
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include "BankAccount.h"
using namespace std;
int main()
{
//Declaring local variables
double amount;
int choice;
//Displaying menu choices
do
{
cout<<"Create an Account"<<endl;
cout<<"1 Checking Account"<<endl;
cout<<"2 Saving Account"<<endl;
cout<<"3 Exit"<<endl;
//reading choice
cin>>choice;
//if checking account
if(choice==1)
{
cout<<"enter account number"<<endl;
int ano;
cin>>ano;
cout<<endl<<"enter opening balance"<<endl;
double obal;
cin>>obal;
double irate;
cout<<endl<<"enter interest rate";
cin>>irate;
CheckingAccount obj (ano,obal,irate);
int ch;
do
{
//ask choice of operations
cout<<"1 Deposit"<<endl<<"2 WithDraw"<<endl<<
"3 Account Info"<<endl<<"4 Exit"<<endl;
cin>>ch;
//Perform selected operation
switch(ch)
{
case 1:
cout<<"Enter amount";
cin>>amount;
double damount;
damount=obj.depositAmount(amount);
cout<<"Available balance: "<<damount<<endl;
break;
case 2:
cout<<"Enter amount";
cin>>amount;
double wamount;
wamount=obj.withDrawAmount(amount);
cout<<"Available balance: "<<wamount<<endl;
break;
case 3:
obj.showAccountInfo();
break;
}//End of switch
}while(ch!=4);
}//end of if
//If choice is savings account
else if(choice==2)
{
//reading needed information
cout<<"enter account number"<<endl;
int ano;
cin>> ano;
cout<<endl<<"enter opening balance"<<endl;
double obal;
cin>> obal;
double irate;
cout<<endl<<"enter interest rate"<<endl;
cin>> irate;
//Creating object for savings account
SavingAccount sObj( ano, obal);
int ch;
do
{
cout<<"1 Deposit"<<endl<<"2 WithDraw"<<endl<<
"3 Account Info"<<"4 Exit"<<endl;
cin>> ch;
switch( ch)
{
case 1:
cout<<"Enter amount";
cin>>amount;
double damount;
damount=sObj.depositAmount(amount);
cout<<"Available balance: "<< damount<<endl;
break;
case 2:
cout<<"Enter amount";
cin>>amount;
double wamount;
wamount=sObj.withDrawAmount(amount);
cout<<"Available balance: "<< wamount<<endl;
break;
case 3:
sObj.showAccountInfo();
break;
case 4:break;
default:cout<<"invalid choice";
break;
}//End of switch
}while( ch!=4);
}//End of else
}while(choice!=3);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.