Question

In: Computer Science

C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...

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

Solutions

Expert Solution

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;
            }

            
        }

Related Solutions

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
In building a regression tree, instead of the mean we can use the median, and instead...
In building a regression tree, instead of the mean we can use the median, and instead of minimizing the squared error we can minimize the absolute error. Why does this help in the case of noise?
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
Write using C++ a) Inputs two 1D arrays of doubles A[i] and B[i] from the keyboard...
Write using C++ a) Inputs two 1D arrays of doubles A[i] and B[i] from the keyboard with a maximum size of 1000. The elements are input one at time alternating between A and B (ie A[0],B[0],A[1],B[1], …, A[i],B[i]) until a value of less than -1.0e6 is input or i >= 1000. Then the program continues to part b). b) Calculates C = A + B, where + is vector addition (ie C[i] = A[i] + B[i]), and prints each element...
In this assignment, we will explore some simple expressions and evaluate them. We will use an...
In this assignment, we will explore some simple expressions and evaluate them. We will use an unconventional approach and severely limit the expressions. The focus will be on operator precedence. The only operators we support are logical or (|), logical and (&), less than (<), equal to (=), greater than (>), add (+), subtract (-), multiply (*) and divide (/). Each has a precedence level from 1 to 5 where higher precedence operators are evaluated first, from left-to-right. For example,...
(Code in C++) 1. We can dynamically resize vectors so that they may grow and shrink....
(Code in C++) 1. We can dynamically resize vectors so that they may grow and shrink. While this is very convenient, it is inefficient. It is best to know the number of elements that you need for your vector when you create it. However, you might come across a situation someday where you do not know the number of elements in advance and need to implement a dynamic vector. Rewrite the following program so that the user can enter any...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT