C++ Inheritance Create a new class that inherits from another class Override a m
ID: 3813355 • Letter: C
Question
C++ Inheritance
Create a new class that inherits from another class
Override a method
Exercise static binding
create the following ::
Write an Account (base) class and CheckingAccount derived class such that the driver code compiles and produces the following output:
DRIVER CODE :
remember
Use the benefits of inheritance as much as possible. For example, there's no need to specify nor implement deposit() or withdraw() in the CheckingAccount class. For the display() method in the CheckingAccount class do make a call to Account's display() (i.e., Account::display()). Additionally, do not have a "balance" data member in the derived class. Note, for the parameterized constructor in the derived class, you'll need to use an initialization list to call Account's parameterized constructor.
OUTPUT ::
Explanation / Answer
PROGRAM CODE:
Account.h
/*
* Account.h
*
* Created on: 08-Apr-2017
* Author: kasturi
*/
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
class Account
{
public:
Account(double);
void deposit(double );
void withdraw(double);
void display();
private:
double balance;
};
#endif
CheckingAccount.h
/*
* CheckingAccount.h
*
* Created on: 08-Apr-2017
* Author: kasturi
*/
#ifndef CHECKINGACCOUNT_H_
#define CHECKINGACCOUNT_H_
#include "Account.h"
class CheckingAccount : public Account
{
public:
CheckingAccount(double);
void processCheck(double, int);
void display();
private:
int lastCheckNumber;
};
#endif /* CHECKINGACCOUNT_H_ */
Account.cpp
/*
* Account.cpp
*
* Created on: 08-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include <iomanip>
#include "Account.h"
using namespace std;
Account::Account(double bal)
{
balance = bal;
}
void Account::deposit(double amount)
{
balance += amount;
}
void Account::withdraw(double amount)
{
balance -= amount;
}
void Account::display()
{
cout<<fixed<<setprecision(2)<<"Account has a balance of $"<<balance<<endl;
}
CheckingAccount.cpp
/*
* CheckingAccount.cpp
*
* Created on: 08-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include "Account.h"
#include "CheckingAccount.h"
using namespace std;
CheckingAccount::CheckingAccount(double bal):Account(bal)
{
lastCheckNumber = 0;
}
void CheckingAccount::processCheck(double amount, int checknumber)
{
withdraw(amount);
lastCheckNumber = checknumber;
}
void CheckingAccount::display()
{
Account::display();
cout<<"Last check number processed: "<<lastCheckNumber<<endl;
}
main.cpp
/*
* main.cpp
*
* Created on: 08-Apr-2017
* Author: kasturi
*/
#include "Account.h"
#include "CheckingAccount.h"
#include <iostream>
using namespace std;
// display the header and then the account details
void displayAccount( Account acct, string header ){
cout << header << endl;
acct.display();
cout << endl;
}
int main(){
Account firstHome( 1234.56 );
CheckingAccount gasMoney( 78.90);
// Note: passing derived classes in the place of the base class
displayAccount( firstHome, "Generic account:");
displayAccount( gasMoney, "Checking account:");
firstHome.deposit( 987.65);
gasMoney.deposit( 12.34);
firstHome.withdraw( 2000.00);
int checkNumber = 234;
gasMoney.processCheck( 55.55, checkNumber);
cout << "Generic account: (after transactions)" << endl;
firstHome.display();
cout << endl;
cout << "Checking account: (after transactions)" << endl;
gasMoney.display();
cout << endl;
return 0;
}
OUTPUT:
Generic account:
Account has a balance of $1234.56
Checking account:
Account has a balance of $78.90
Generic account: (after transactions)
Account has a balance of $222.21
Checking account: (after transactions)
Account has a balance of $35.69
Last check number processed: 234
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.