Question

In: Computer Science

C++ Assignment: For this assignment, we will model a checking account and savings account, arranging these...

C++ Assignment:

For this assignment, we will model a checking account and savings account, arranging these two types of accounts within a simple class hierarchy beneath a base class. By creating a class hierarchy, we will avoid unnecessary code duplication across the derived classes and create an intuitive relationship between them. We will expand on this assignment in a future assignment.


Create a class named Account, which will contain data and methods that would naturally be shared by any derived classes. Remember to create the class using two files (Account.h and Account.cpp) in order to separate the interface from the implementation. Since any type of account (whether we’re talking about a checking account, savings account, or anything else for that matter) will contain certain, basic data, such as the account number, the account holder’s name, and the current balance, we can add these data members into this base class. Mark each of the data members listed below with a protected access specifier.


Create a data member of type double to represent the account balance, along with a corresponding setter method.


Create a data member of type unsigned int to represent the account number.


Create two data members of type std::string to represent the account holder’s first and last names, along with corresponding setter methods.


Create a static unsigned int (initialized to 0) to represent the number of Account objects that have been create Note that you will need to edit both the Account.h file and the corresponding Account.cpp file to accomplish this step; refer to pages 429-33 for an example of how this is done.


Give the Account class a default constructor that increments the account tally contained in the static variable from the previous step (item d) and uses it to set the account number field of the Account class (item b).


Give the Account class a method called display() that prints the account balance, account number, and the account holder’s name to the screen.


Create a class named Checking. Again, create the class using two files (so you’ll have Checking.h and Checking.cpp).


The Checking class should use public inheritance to derive from the Account class.


Create a private data member of type bool to represent whether the account has been signed up for overdraft protection. Create a setter method for this data member.


Give the Checking class a method named display(), which first invokes the Account class’s version of display() and then displays the overdraft protection status for the Checking object.


Create a class named Savings. Again, create the class using two files (so you’ll have Savings.h and Savings.cpp).


The Savings class should use public inheritance to derive from the Account class.


Create a private data member of type double to represent the interest rate associated with the savings account, along with both a corresponding setter and getter .


Give the Savings class a method named display(), which first invokes the Account class’s version of display() and then displays the interest rate for the Savings object.


In a separate driver file, demonstrate the behavior of your hierarchy in the following way:

Create an instance of a checking account type on the heap, storing the address in a pointer. Use new to do this. This pointer can then be used to set the values stored in the object. Set the first name, last name, balance, and overdraft protections to values of your choosing. The account number is set by the constructor. Call display then delete the pointer (using delete).


Create an instance of a savings account type on the heap, storing the address in a pointer. Use new to do this. This pointer can then be used to set the values stored in the object. Set the first name, last name, balance, and interest rate to values of your choosing. The account number is set by the constructor. Call display then delete the pointer (using delete).


Sample run:


Balance: 1000.00


Account: 1


First Name: Alice


Last Name: Meacham


Overdraft Protection: true


Balance: 1003.00


Account: 2


First Name: Bob


Last Name: Ferling


Interest rate: 5.00


Solutions

Expert Solution

// Account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H

#include <string>

class Account
{
private:
unsigned int accountNumber;
std::string firstName, lastName;
double balance;
static unsigned int count;

public:
Account();
void setFirstName(std::string fName);
void setLastName(std::string lName);
void setBalance(double inBalance);
void display();
};

#endif // ACCOUNT_H

// end of Account.h

// Account.cpp

#include "Account.h"
#include <iostream>
using namespace std;

// initialize the static counter to 0
unsigned int Account::count = 0;

// default constructor
Account::Account()
{
count++; // increment count
accountNumber = count; // set accountNumber to count
}

// set the first name
void Account:: setFirstName(std::string fName)
{
firstName = fName;
}

// set the last name
void Account:: setLastName(std::string lName)
{
lastName = lName;
}

// set the balance
void Account:: setBalance(double inBalance)
{
balance = inBalance;
}

// display the details of the Account
void Account:: display()
{
cout<<fixed;
cout.precision(2); // show 2 digits after decimal
cout<<"Balance: "<<balance<<endl;
cout<<"Account: "<<accountNumber<<endl;
cout<<"First Name: "<<firstName<<endl;
cout<<"Last Name: "<<lastName<<endl;
}

//end of Account.cpp

// Checking.h

#ifndef CHECKING_H
#define CHECKING_H

#include "Account.h"

class Checking: public Account
{
private:
bool overdraft_protection;
public:
void setOverdraftProtection(bool op);
void display();
};

#endif // CHECKING_H

//end of Checking.h

// Checking.cpp

#include "Checking.h"
#include <iostream>
using namespace std;

// set the overdraft protection
void Checking:: setOverdraftProtection(bool op)
{
overdraft_protection = op;
}

// display the details of Checking Account
void Checking:: display()
{
Account::display(); // call Account's display
cout<<"Overdraft Protection: "<<boolalpha<<overdraft_protection<<endl; // display overdraft protection status
}

//end of Checking.cpp

// Savings.h

#ifndef SAVINGS_H
#define SAVINGS_H

#include "Account.h"

class Savings : public Account
{
private:
double interest_rate;

public:
void setInterestRate(double rate);
double getInterestRate();
void display();
};


#endif // SAVINGS_H

//end of Savings.h

// Savings.cpp

#include "Savings.h"
#include <iostream>

using namespace std;

// set the interest rate
void Savings:: setInterestRate(double rate)
{
interest_rate = rate;
}

// return the interest rate
double Savings:: getInterestRate()
{
return interest_rate;
}

// display details of Savings Account
void Savings:: display()
{
Account::display(); // call Account's display method
cout<<"Interest rate: "<<interest_rate<<endl; // display interest rate
}

//end of Savings.cpp

// driver.cpp : C++ program to test the Checking and Savings class

#include <iostream>
#include "Checking.h"
#include "Savings.h"

using namespace std;

int main()
{
// Create an instance of a checking account type on the heap
Checking *check = new Checking();
// set the values for Checking Account
check->setBalance(1000);
check->setFirstName("Alice");
check->setLastName("Meacham");
check->setOverdraftProtection(true);

// display the details
check->display();
cout<<endl;
delete check; // delete the pointer (using delete).

// Create an instance of a savings account type on the heap
Savings* savings = new Savings();
// set the values for Savings Account
savings->setBalance(1003);
savings->setFirstName("Bob");
savings->setLastName("Ferling");
savings->setInterestRate(5);
// display the details
savings->display();

delete savings; // delete the pointer

return 0;
}

//end of driver.cpp

Output:


Related Solutions

1) Please explain the following terms: savings account, basic savings account, interest bearing checking account, money...
1) Please explain the following terms: savings account, basic savings account, interest bearing checking account, money market deposit accounts, and certificate of deposits 2) What is the difference between a bond and a certificate deposit
Describe some examples of checking and savings account transactions that result in assessments of fees or...
Describe some examples of checking and savings account transactions that result in assessments of fees or penalties. Which are the least and most avoidable? 2. Analyze your personal budget as a financial planning tool for making decisions in the following situations. In each case, how will they affect your budget (consider each individually)? a. A neighbor and coworker suggest that he and you commute to work together. b. The roofers inform you that your chimney needs be to repointed and...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes. This is a sample of the txt...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes vertical columned categories. This is a sample...
C++ 7th edition Banks offer various types of accounts, such as savings, checking, certificate of deposits,...
C++ 7th edition Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you...
c++ The above Account class was created to model a bank account. An account has the...
c++ The above Account class was created to model a bank account. An account has the properties (keep the properties private) account number, balance, and annual interest rate, date created, and functions to deposit and withdraw. Create two derived classes for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Define a constant virtual toString() function in the Account class and override it in the derived classes to return the account...
In our construction of the savings and investment model, we considered government spending to be immediate...
In our construction of the savings and investment model, we considered government spending to be immediate spending. In other words, there was no “investment” component to government spending. Let’s change up that assumption. Suppose government spending comes in two types: investment spending (new airports, for example) as well as government consumption (snow plowing, for example). Call the first GI and the second GCE. a. Derive the savings and investment equations under this new assumption and prove, once again, that in...
Distinguish among the following: a general checking account, an imprest bank account, and a lockbox account.
Distinguish among the following: a general checking account, an imprest bank account, and a lockbox account.
Suppose you deposit $1500 cash into your checking account. By how much will checking deposits in...
Suppose you deposit $1500 cash into your checking account. By how much will checking deposits in the banking system increase as a result when the required reserve ratio is 0.50?
In C++ In this assignment, we will implement a square root approximater and then an nth...
In C++ In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of x is the number when raised to the power n gives x. We can use the concepts of binary search to approximate the square root of a number, and we can continue this logic to approximate the nth square root. We're going to use the concepts of binary search to try and approximate ending the square...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT