Completely Solved C, C++ Programs Assignment.




C++ program to create an inheritance hierarchy of banking system

Filed Under:

Problem Statement :
Create an inheritance hierarchy that a bank might use to represent customers' bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e., credit or debit).

Logical Approach :
The above program is done using an Abstract Data Type (ADT) called class. A class is a mechanism to create objects. We have used inheritance feature in this program. The mechanism of deriving a new class(derived class) from an old one(base class) is called inheritance. This program is based on hierarchical inheritance. In this program a base class named Account is created ,
which contains the following data members :
 acc_blnc
and following member functions :
 Account()- Parameterized constructor, used to initialize account balance.
 credit()-Used to add an amount to the current balance.
 debit()-Used to withdraw money from the Account.
 getBalance()-Used to return the current balance.
 assgn()-Used to assign current balance to the account.

SavingsAccount is another class which is also derived from Account class and contains the following data member :
 interest_rate
and following member functions :
 SavingsAccount()- Parameterized constructor, used to initialize interest rate and receive initial balance.
 CalculateInterest()- Used to return the amount of interest earned by an account.

CheckingAccount is another class derived from both exam and sports which contains following data member :
 fee
and following member functions :
 CheckingAccount()-Parameterized constructor, used to initialize fee charged per transaction and receive initial balance.
 credit()- This function should charge a fee only if money is actually added.
 debit()-This function should charge a fee only if money is actually withdrawn.

Algorithm :
Creating an ADT with name Account

Step 1 : Creating an ADT Account
Step 2 : Declaring protected data members
double acc_blnc; //Declaring a double
Step 3 : Declaring public member functions
 Account(double)
 credit()
 debit()
 getBalance()
 assgn(double)

Parameterized constructor, used to initialize account balance

Account(blnc)
{
if(blnc<=0.0)
{
Print "\nInvalid amount entered!!\n";
acc_blnc=0.0;
}
else
acc_blnc=blnc;
}

This function is used to add an amount to the current balance

credit(amount)
{
if(amount<=0.0)
{
Print "Transaction unsucessful!!Please enter a valid amount.";
return false;
}
acc_blnc+=amount;
Print "Transaction completed succcessfully.";
return true;

}


This function is used to withdraw money from the Account

debit(amount)
{
if(amount<=0.0)
{
Print "Transaction unsucessful!!Please enter a valid amount.";
return false;
}
if(amount>acc_blnc)
{
Print "Transaction unsucessful!!Debit amount exceeded account balance.";
return false;
}
acc_blnc-=amount;
Print "Transaction completed succcessfully.";
return true;
}

This function is used to return the current balance

getBalance()
{
return acc_blnc;
}

This function is used to assign current balance to the account

assgn(bl)
{
acc_blnc=bl;
}

Creating an ADT with name SavingsAccount

Step 1 : Creating an ADT SavingsAccount
Step 2 : Declaring protected data members
double interest_rate; //Declaring a double
Step 3 : Declaring public member functions
&#61548; SavingsAccount()
&#61548; CalculateInterest()

Parameterized constructor, used to initialize interest rate and receive initial balance

SavingsAccount(balance,percentage):Account(balance)
{
acc_blnc=balance;
interest_rate=percentage;
}

This function is used to return the amount of interest earned by an account

CalculateInterest()
{
return acc_blnc*(interest_rate/100);
}

Creating an ADT with name CheckingAccount

Step 1 : Creating an ADT CheckingAccount
Step 2 : Declaring protected data members
double fee; //Declaring a double
Step 3 : Declaring public member functions
&#61548; CheckingAccount(double,double)
&#61548; credit()
&#61548; debit()

Parameterized constructor, used to initialize fee charged per transaction and receive initial balance

CheckingAccount(balance,fee_amount):Account(balance)
{
acc_blnc=balance;
fee=fee_amount;
}

This function should charge a fee only if money is actually added

credit(amount)
{
if((amount-fee)>=0.0)
{ amount-=fee;
Print "Fee charged succcessfully.";
}
else
amount=-99;
return amount;
}

This function should charge a fee only if money is actually withdrawn

debit(amount)
{
if((amount-fee)>=0.0)
{
amount-=fee;
Print "Fee charged succcessfully.";
}
else
amount=-99;
return amount;
}

Main procedure

main()
{
double bal;
Read initial account balance from user;
Account ac(bal);
i_rate=4.0,fee=30.00;
while(true)
{
//Display Bank menu
1.Administrator.
2.Normal user.
3.Exit.
Read choice ch from user
switch(ch)
{
case 1:
//Display Bank sub-menu
1.Change interest rate.
2.Change fee charged per transaction.
3.Add interest to account.
Read choice from user
switch(ch)
{
case 1:
Read new interest rate
break;
case 2:
while(true)
{
Read a new valid amount of fee charged per transaction
if(fee>=0.0)
break;
Print "Invalid amount of fee entered!!"
}
break;
case 3:
{
SavingsAccount sav(ac.getBalance(),i_rate);
double interest=sav.CalculateInterest();
if((ac.credit(interest)==true))
Print "Interest added successfully."
}
break;
default:
Print "Invalid choice entered!!"
}
break;
case 2:
//Display Bank sub-menu
1.Deposit balance.
2.Withdraw balance.
Read choice from user
switch(ch)
{
case 1:
{
CheckingAccount acc(ac.getBalance(),fee);
Read amount to deposit
if((ac.credit(bal)==true))
{
bal=acc.credit(ac.getBalance());
if(bal==-99)
{
Print "Fee charge is not successful,operation terminated!!"
return 0;
}
ac.assgn(bal);
}
}
break;
case 2:
{
CheckingAccount acc(ac.getBalance(),fee);
Read amount to be withdrawn
if((ac.debit(bal)==true))
{
bal=acc.debit(ac.getBalance());
if(bal==-99)
{
Print "Fee charge is not successful,operation terminated!!"
return 0;
}
ac.assgn(bal);
}
}
break;
default:
Print "Invalid choice entered!!"
}
break;
case 3:
return 0;
default:
Print "Invalid choice entered!!"
}

}
}

Program listing :
//C++ program to implement an inheritance hierarchy of a bank

#include<iostream>

using namespace std;
//A base class named Account is created
class Account
{
protected:
double acc_blnc;
public:
Account(double);
bool credit(double);
bool debit(double);
double getBalance();
void assgn(double);
};
//Parameterized constructor, used to initialize account balance
Account::Account(double blnc)
{
if(blnc<=0.0)
{
cout<<"\nInvalid amount entered!!\n";
acc_blnc=0.0;
}
else
acc_blnc=blnc;
}
//This function is used to assign current balance to the account
void Account::assgn(double bl)
{
acc_blnc=bl;
}
//This function is used to add an amount to the current balance
bool Account::credit(double amount)
{
if(amount<=0.0)
{
cout<<"\nTransaction unsucessful!!Please enter a valid amount.\n";
return false;
}
acc_blnc+=amount;
cout<<"\nTransaction completed succcessfully...\n";
return true;

}
//This function is used to withdraw money from the Account
bool Account::debit(double amount)
{
if(amount<=0.0)
{
cout<<"\nTransaction unsucessful!!Please enter a valid amount.\n";
return false;
}
if(amount>acc_blnc)
{
cout<<"\nTransaction unsucessful!!Debit amount exceeded account balance...\n\n";
return false;
}
acc_blnc-=amount;
cout<<"\nTransaction completed succcessfully...\n";
return true;
}
//This function is used to return the current balance
double Account::getBalance()
{
return acc_blnc;
}
//Derived class named SavingsAccount is created which is derived from base class Account
class SavingsAccount:public Account
{
protected:
double interest_rate;
public:
SavingsAccount(double,double);
double CalculateInterest();
};
//Parameterized constructor, used to initialize interest rate and receive initial balance
SavingsAccount::SavingsAccount(double balance,double percentage):Account(balance)
{
acc_blnc=balance;
interest_rate=percentage;
}
//This function is used to return the amount of interest earned by an account
double SavingsAccount::CalculateInterest()
{
return acc_blnc*(interest_rate/100);
}
//Derived class named CheckingAccount is created which is derived from base class Account
class CheckingAccount:public Account
{
protected:
double fee;
public:
CheckingAccount(double,double);
double credit(double);
double debit(double);
};
//Parameterized constructor, used to initialize fee charged per transaction and receive initial balance
CheckingAccount::CheckingAccount(double balance,double fee_amount):Account(balance)
{
acc_blnc=balance;
fee=fee_amount;
}
//This function should charge a fee only if money is actually added
double CheckingAccount::credit(double amount)
{
if((amount-fee)>=0.0)
{ amount-=fee;
cout<<"\nFee charged succcessfully...\n";
}
else
amount=-99;
return amount;
}
//This function should charge a fee only if money is actually withdrawn
double CheckingAccount::debit(double amount)
{
if((amount-fee)>=0.0)
{
amount-=fee;
cout<<"\nFee charged succcessfully...\n";
}
else
amount=-99;
return amount;
}

int main()
{
double bal;
cout<<"Enter initial account balance:";
cin>>bal;
Account ac(bal);
double i_rate=4.0,fee=30.00;
cout<<"\n\n";
int ch;
while(true)
{
cout<<"\nBANK\n---------------------\n""\n1.Administrator.\n2.Normal user.\n3.Exit.\n\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n1.Change interest rate.\n2.Change fee charged per transaction.\n3.Add interest to account\n\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter new interest rate:";
cin>>i_rate;
break;
case 2:
while(true)
{
cout<<"Enter a new valid amount of fee charged per transaction:";
cin>>fee;
if(fee>=0.0)
break;
cout<<"\nInvalid amount of fee entered!!\n";
}
break;
case 3:
{
SavingsAccount sav(ac.getBalance(),i_rate);
double interest=sav.CalculateInterest();
if((ac.credit(interest)==true))
cout<<"\nInterest added successfully.\n";
}
break;
default:
cout<<"Invalid choice entered!!\n";
}
break;
case 2:
cout<<"\n1.Deposit balance.\n2.Withdraw balance.\n\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
{
CheckingAccount acc(ac.getBalance(),fee);
cout<<"Enter amount to deposit:";
cin>>bal;
if((ac.credit(bal)==true))
{
bal=acc.credit(ac.getBalance());
if(bal==-99)
{
cout<<"\nFee charge is not successful,operation terminated!!\n";
return 0;
}
ac.assgn(bal);
}
}
break;
case 2:
{
CheckingAccount acc(ac.getBalance(),fee);
cout<<"Enter amount to withdraw:";
cin>>bal;
if((ac.debit(bal)==true))
{
bal=acc.debit(ac.getBalance());
if(bal==-99)
{
cout<<"\nFee charge is not successful,operation terminated!!\n";
return 0;
}
ac.assgn(bal);
}
}
break;
default:
cout<<"Invalid choice entered!!\n";
}
break;
case 3:
return 0;
default:
cout<<"Invalid choice entered!!\n";
}

}
}

Output:
Enter initial account balance:12000

BANK
---------------------

1.Administrator.
2.Normal user.
3.Exit.

Enter your choice:2

1.Deposit balance.
2.Withdraw balance.

Enter your choice:1
Enter amount to deposit:2000

Transaction completed succcessfully...

Fee charged succcessfully...

BANK
---------------------

1.Administrator.
2.Normal user.
3.Exit.

Enter your choice:1

1.Change interest rate.
2.Change fee charged per transaction.
3.Add interest to account

Enter your choice:2
Enter a new valid amount of fee charged per transaction:10.50

BANK
---------------------

1.Administrator.
2.Normal user.
3.Exit.

Enter your choice:2

1.Deposit balance.
2.Withdraw balance.

Enter your choice:2
Enter amount to withdraw:3450

Transaction completed succcessfully...

Fee charged succcessfully...

BANK
---------------------

1.Administrator.
2.Normal user.
3.Exit.

Enter your choice:1

1.Change interest rate.
2.Change fee charged per transaction.
3.Add interest to account

Enter your choice:3

Transaction completed succcessfully...

Interest added successfully.

BANK
---------------------

1.Administrator.
2.Normal user.
3.Exit.

Enter your choice:3


Discussions :
&#61548; Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

&#61548; All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

&#61548; C++ strongly supports the concept of reusability. Inheritance is an way to reuse classes.

&#61548; There are many types or forms of inheritance- Single, multiple, multilevel, hierarchical and hybrid.

&#61548; In this program all derivation of derived classes from base class are made public.

&#61548; Fee per transaction and interest rate can be changed in Administrator menu.




Back to main directory:  C++ Assignment    Software Practical    Banking System


Get Free Programming Tutorials and Solved assignments