Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Inheritance and compositions Please read through the whole document, and then st

ID: 3894580 • Letter: I

Question

Inheritance and compositions

Please read through the whole document, and then start to program.

Assume that a bank maintains two types of accounts for customers, saving and checking. The saving account provides compound interest and withdrawal facility. The current account provides cheque book facility but no interest. Current account holders should also maintain a minimum balance and if the balance falls below this level, a service charge is imposed. The saving account does not have to maintain a minimum balance but a service fee will be charged each time withdraw money directly from the saving account. Create a class account that stores customer account number (constant integer), type of account, balance, minim_balance, and over_limit_charge.

Inherit the account class into classes chk and sve, respectively. In the sve class, declare a member variable called fixed_each_time_charge that stores each time withdraw service rate. Also create a user class to maintain the user profile, which has members bankname, username, chk1, and saving1. Include the necessary member functions and constructors to achieve the following tasks:

For the class account:

• account(): the default constructor which generates an account number using random integer, and initialize other member variables.

• account(in no): the overloaded constructor that takes the argument for account number and initialize other member variables.

• void display(): displays (print) the account number and the balance.

• void deposit(float m): accept deposit from a customer and update the balance.

• virtual void withdraw(float m){}: define virtual function that can be override by the derived class and enable late biding to dynamically call corresponding member functions in the derived classes.

• float getbalance(): return the balance

In the checking chk class:

• define two constructors corresponding to the parent class account

• define a withdraw function that withdraws money according to the user request In this function, check if this withdraw is allowed according to the available balance and withdraw limit for each time. Calculate over limit service fee if the balance is lower than a limit when applying a withdraw. Update the balance

In the sve class:

• define two constructors corresponding to the parent class account

• define a withdraw function that withdraws money according to the user request In this function, check if this withdraw is allowed according to the available balance for each time. Apply a service fee for each time withraw. Update the balance

In the user class:

• define a constructor with a string argument for user name • void display(): displays (print) the user number and the total balance (the sum of checking and saving balances).

• chk& getchk(): return the checking account object

• sve& getsve(): return the saving account object

Create a test main function that displays a two-levels menu and take input for menu item selection as follows. The code is provided in Lab7_test.cpp. Pay attention to the function void submenu(account& acc), which will dynamically call the withdraw function for according to the argument object.

First level:

Please select a function (0-3):

0---exit

1---display balance

2---go to checking

3---go to saving

Second level:

0---return to upper menu

1---display balance

2---deposit

3---withdraw

Note: Except withdraw, all other member functions and constructor should be in-lines. Declare the withdraw function in Lab7.h and implement (define) it in the Lab7.cpp file. Please download skeleton files Lab7.cpp, Lab7.h, and Lab7_test.cpp from C4. Mostly you will be working on Lab7.cpp and Lab7.h, but please carefully read through and understand the role and content of all three files. Please create a project in Eclipse and import all the 3 files.

/*
* Lab7.cpp
*
*
* NAME:
* STUDENT No:
*/
#include "Lab7.h"

//here define the withdraw function for class chk


//here define the withdraw function for class sve

/*
* Lab7_test.cpp
* NANE:
* STUDENT NUMBER:
*/
#include "Lab7.h"
void submenu(account& acc){
int menu;
while(1){
cout << "Please select a function (0-3): " << endl;
cout << "0---return to upper menu " << endl;
cout << "1---display balance " << endl;
cout << "2---deposit " << endl;
cout << "3---withdraw " << endl;
cin >> menu;
switch (menu){
case 0:return;
case 1:
//display the balance of the current account
acc.display();
break;
case 2:
float m;
cout << "Input the amount of cash you want ot deposit: " << endl;
cin >> m;
acc.deposit(m);
break;
case 3:
cout << "Input the amount of cash you want ot withdraw: " << endl;
cin >> m;
acc.withdraw(m);
break;
}
}
}

int main(){
srand (time(NULL));
user user1("John");
// input and select the menu
while(1){
// build the menu
int menu;
cout << "Please select a function (0-3): " << endl;
cout << "0---exit " << endl;
cout << "1---display balance " << endl;
cout << "2---go to checking " << endl;
cout << "3---go to saving " << endl;
cin >> menu;
switch (menu){
case 0:return 0;
case 1:
//display the total balance
user1.display();
break;
case 2:
submenu(user1.getchk());
break;
case 3:
submenu(user1.getsve());
break;
}
}
}

/*
* Lab7.h
*
* NAME:
* STUDENT No:
*/

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
enum acctypes{ saving,checking};
class account{
protected:
const int accountNo;
acctypes acctype;
float balance;
float minimum_banlance;
float over_limit_charge;
public:
account():accountNo(rand()),minimum_banlance(1000),over_limit_charge(3.9){
balance = 0;
}
account(int no):accountNo(no),minimum_banlance(1000),over_limit_charge(3.9){
balance = 0;
}
void display(){
cout << "Acount number: " << accountNo << endl;
cout << "Balance: " << balance << endl;
}
void display(string username){
cout << "User name: " << username << endl;
cout << "Acount number: " << accountNo << endl;
cout << "Balance: " << balance << endl;
}
void deposit(float m){
balance += m;
}
virtual void withdraw(float m){
  
}
int getno(){return accountNo;}
float getbalance(){return balance;}
};
//class chk goes here


//class sve goes here

class user{//user profile
string bankname;
string username;
chk chk1;
sve saving1;
public:
user(string u){username = u;}
void display(){
cout << "User name: "
<< username << endl;
cout << "Total Balance: "
<< chk1.getbalance()+saving1.getbalance() << endl;
}
chk& getchk(){
return chk1;
}
sve& getsve(){
return saving1;
}
};

Explanation / Answer

//Lab7.h

/*

* Lab7.h

*

* NAME:

* STUDENT No:

*/

#include <iostream>

#include <string>

#include <stdlib.h>

#include <time.h>

using namespace std;

enum acctypes { saving, checking };

class account {

protected:

const int accountNo;

acctypes acctype;

float balance;

float minimum_balance;

float over_limit_charge;

public:

account() :accountNo(rand()), minimum_balance(1000), over_limit_charge(3.9) {

balance = 0;

}

account(int no) :accountNo(no), minimum_balance(1000), over_limit_charge(3.9) {

balance = 0;

}

void display() {

cout << "Account number: " << accountNo << endl;

cout << "Balance: " << balance << endl;

}

void display(string username) {

cout << "User name: " << username << endl;

cout << "Account number: " << accountNo << endl;

cout << "Balance: " << balance << endl;

}

void deposit(float m) {

balance += m;

}

virtual void withdraw(float m) {

}

int getno() { return accountNo; }

float getbalance() { return balance; }

};

//class chk goes here

class chk :public account {

private:

double withdraw_limit;

public:

chk() : account(){

acctype = checking;

}

chk(int no) :account(no){

acctype = checking;

}

void withdraw(float m);

};

//class sve goes here

class sve :public account {

private:

float fixed_each_time_charge;

public:

sve() : account(), fixed_each_time_charge(10.0) {

acctype = saving;

}

sve(int no) :account(no), fixed_each_time_charge(10.0) {

acctype = saving;

}

void withdraw(float m);

};

class user {//user profile

string bankname;

string username;

chk chk1;

sve saving1;

public:

user(string u) { username = u; }

void display() {

cout << "User name: "

<< username << endl;

cout << "Total Balance: "

<< chk1.getbalance() + saving1.getbalance() << endl;

}

chk& getchk() {

return chk1;

}

sve& getsve() {

return saving1;

}

};

//Lab7.cpp

/*

* Lab7.cpp

*

*

* NAME:

* STUDENT No:

*/

#include "Lab7.h"

//here define the withdraw function for class chk

void chk::withdraw(float m)

{

//If withdrawal amount is > balance, withdrawal fails

if (m > balance)

return;

//Impose a service charge, if balance falls below minumum balance

if ((balance - m) < minimum_balance)

{

balance -= (m + over_limit_charge);

}

}

//here define the withdraw function for class sve

void sve::withdraw(float m)

{

if ((m + fixed_each_time_charge) <= balance)

{

balance -= (m + fixed_each_time_charge);

}

}