In: Computer Science
C++ Assignment Inheritance
This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine
We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below.
Phase 1 :
Here is the following set of classes you will implement and their inheritance relationship:
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 parameter
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.
Checking balance: $100 Savings balance: $100 Credit Card balance: $100
1. Savings Deposit
2. Savings withdrawal
3. Checking Deposit
4. Write A Check
5. Credit Card Payment
6. Make A Charge
7. Display Savings
8. Display Checking
9. Display Credit Card
0. Exit
The following is the full answer to the above question. It uses
some sentences so as to guide the user step by step. Various other
operations can be performed accept than the mentioned
functionality, it is up to the requirement of the code. The
capability of a class to derive properties and characteristics from
another class is called Inheritance. It is a very
popular concept of Object oriented programming. In
Inheritance:
Sub Class: The class that inherits properties from
another class is called Sub class or Derived Class.
Super Class:The class whose properties are
inherited by sub class is called Base Class or Super class.
Below code contains "Account" as the base class and "Checking", "Savings" and "Credit Card" and derived form this base class.
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Account //Base Class
{
private:
string name;
long taxID;
double balance;
protected:
double last10withdraws[10] = { 0.0 };
double last10deposits[10] = { 0.0 };
int numdeposits =0;
int numwithdraws= 0;
public:
void SetName( string username) //Set the Name of the owner of the bank account
{
name = username;
}
void SetTaxID( long userTaxID) //Set the Tax ID of the owner of the bank account
{
taxID = userTaxID;
}
void Setbalance (double userBal) //Set the Balance of bank account
{
balance = userBal;
}
string GetName() //Get the Name of the owner of the bank account
{
return name;
}
double GetTaxID() //Get the Tax ID of the owner of the bank account
{
return taxID;
}
double Getbalance() //Get the balance of the bank account
{
return balance;
}
void MakeDeposit( double amount) //Make deposits into the bank account. (Any Kind of Bank Account)
{
balance += amount;
last10deposits[numdeposits] = amount;
numdeposits++;
}
Account(){} //Constructor with no defination.
Account(string name,long taxID,double balance) // Parameterized Constructor with a defination that initialize the object with private variables with that of the passed values.
{
name = name;
taxID = taxID;
balance = balance;
}
void display() //Displays the Name ,Tax ID and Balance of the Bank Account.
{
cout<<"Name :"<<name<<endl;
cout<<"Tax ID :"<<taxID<<endl;
cout<<"Account Balance :"<<balance<<endl;
}
};
class Checking : public Account //Checkings account - Derived class from the base class
{
private:
int last10checks[10] = { -1 };
int numcheck = 0;
public:
void WriteCheck(int checknum,double amount) // Writes check of the amount and the check number passed and adjusts the balance accordingly.
{
int balance = Getbalance();
Setbalance(balance - amount);
last10withdraws[numwithdraws] = amount;
numwithdraws++;
last10checks[numcheck] = checknum;
numcheck++;
}
Checking(){} //Default constructor
Checking(string name,long taxID,double balance) //Parameterized Constructor
{
name = name;
taxID = taxID;
balance = balance;
}
void display() //Displays the Checks Record and the Deposits records of the Checking Bank Account
{
cout<<"Checks record -"<<endl;
for(int i = 0 ; i<numwithdraws; i++)
{
cout<<"Check number : "<<last10checks[i]<<" ,amount withdrawn : "<<last10withdraws[i]<<endl;
}
cout<<"Deposits record -"<<endl;
for(int i = 0 ; i<numdeposits; i++)
{
cout<<last10deposits[i]<<endl;
}
}
};
class Savings : public Account //Savings account - Derived class from the base class
{
public:
void DoWithdraw(double amount) //Withdraws the passed amount form the savings amount and adjusts the balance accordingly
{
int balance = Getbalance();
Setbalance(balance - amount);
last10withdraws[numwithdraws] = amount;
numwithdraws++;
}
Savings(){} //Default Constructor
Savings(string name,long taxID,double balance) //Parameterized Constructor
{
name = name;
taxID = taxID;
balance = balance;
}
void display() //Displays the Withdrawal Record and the Deposits records of the Savings Bank Account
{
cout<<"Withdrawal Record -"<<endl;
for(int i = 0 ; i<numwithdraws; i++)
{
cout<<last10withdraws[i]<<endl;
}
cout<<"Deposits Record -"<<endl;
for(int i = 0 ; i<numdeposits; i++)
{
cout<<last10deposits[i]<<endl;
}
}
};
class CreditCard : public Account //Credit Card account - Derived class from the base class
{
public:
long cardnumber;
vector<string> last10charges;
void DoCharge(string name , double amount) //Charges the Credit card the passed amount and updates balance accordingly.
{
int balance = Getbalance();
Setbalance(balance - amount);
last10withdraws[numwithdraws] = amount;
numwithdraws++;
last10charges.push_back(name);
}
void MakePayment(double amount) //Does payements for the Credit card.
{
MakeDeposit(amount);
}
CreditCard(){} //Default Constructor
CreditCard(string name,long taxID,double balance)//Parameterized Constructor
{
name = name;
taxID = taxID;
balance = balance;
}
void display() //Displays the Charges Record and the Deposits records of the Credit Card Bank Account
{
cout<<"Charges Record -"<<endl;
for(int i = 0 ; i<numwithdraws; i++)
{
cout<<"Charge name : "<<last10charges[last10charges.size()-numwithdraws+i]<<" ,amount charged : "<<last10withdraws[i]<<endl;
}
cout<<"Deposits Record -"<<endl;
for(int i = 0 ; i<numdeposits; i++)
{
cout<<last10deposits[i]<<endl;
}
}
};
int main()
{
Checking checkingObj; //Initializing 3 objects of different types of account and setting the balance of each bank account as $100.
checkingObj.Setbalance(100.00);
Savings savingsObj;
savingsObj.Setbalance(100.00);
CreditCard creditcardObj;
creditcardObj.Setbalance(100.00);
//It is possible to set the account names and tax id using similar method.
//To view total balance, tax ID, and the name of an account, make a call to the display function of the base class.
//This can be achieved by calling ,for example, savings.Account::display(), and similar calls for all the differnt kind of bank accounts;
cout<<" MENU -"<<endl;
cout<<"1. Savings Deposit"<<endl<<"2. Savings withdrawal"<<endl<<"3. Checking Deposit"<<endl<<"4. Write A Check"<<endl<<"5. Credit Card Payment"<<endl<<"6. Make A Charge"<<endl<<"7. Display Savings"<<endl<<" 8. Display Checking"<<endl<<"9. Display Credit Card"<<endl<<"0. Exit"<<endl;
int selection;
cin>>selection;
while(selection != 0)
{
if(selection == 1)
{
double amount;
cout<<"Enter Amount to be deposoted in Savings account."<<endl;
cin>>amount;
savingsObj.MakeDeposit(amount);
savingsObj.display();
}
else if(selection == 2)
{
double amount;
cout<<"Enter Amount to withdraw from Savings account."<<endl;
cin>>amount;
savingsObj.DoWithdraw(amount);
savingsObj.display();
}
else if(selection == 3)
{
double amount;
cout<<"Enter Amount to be deposoted in Checking account."<<endl;
cin>>amount;
checkingObj.MakeDeposit(amount);
checkingObj.display();
}
else if(selection == 4)
{
double amount;
int checknum;
cout<<"Enter Check number and Amount to to be written on check."<<endl;
cin>>checknum>>amount;
checkingObj.WriteCheck(checknum,amount);
checkingObj.display();
}
else if(selection == 5)
{
double amount;
cout<<"Enter Payment Amount."<<endl;
cin>>amount;
creditcardObj.MakePayment(amount);
creditcardObj.display();
}
else if(selection == 6)
{
double amount;
string name;
cout<<"Enter Charge Name and Charge amount"<<endl;
cin>>name>>amount;
creditcardObj.DoCharge(name,amount);
creditcardObj.display();
}
else if( selection == 7)
{
savingsObj.display();
}
else if( selection == 8)
{
checkingObj.display();
}
else if( selection == 9)
{
creditcardObj.display();
}
else
{
cout<<"Invalid Selection"<<endl;
return 0;
}
cout<<"1. Savings Deposit"<<endl<<"2. Savings withdrawal"<<endl<<"3. Checking Deposit"<<endl<<"4. Write A Check"<<endl<<"5. Credit Card Payment"<<endl<<"6. Make A Charge"<<endl<<"7. Display Savings"<<endl<<" 8. Display Checking"<<endl<<"9. Display Credit Card"<<endl<<"0. Exit"<<endl;
cin>>selection;
}
}