In: Computer Science
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
// 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: