In: Computer Science
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
#include <iostream>
using namespace std;
// Creating base class Account
class Account
{
private:
// Declaring variables
string name;
int accNo;
double balance;
public:
// parameterized constructor
Account(string name,int accNo,double balance)
{
this->name=name;
this->accNo=accNo;
if (balance > 0.0)
{
this->balance = balance;
}
else
{
this->balance = 0.0;
cout << "** Initial balance was invalid **" <<
endl;
}
}
// Function declarations
void deposit(double amount);
bool withdraw(double amount);
double getBalance();
void setBalance(double amt);
};
/* Function implementation
* which gets current balance
*/
double Account::getBalance()
{
return this->balance;
}
void Account::setBalance(double amt)
{
this->balance = amt;
}
void Account::deposit(double amount)
{
balance+=amount;
}
bool Account::withdraw(double amount)
{
if(balance>=amount)
{
balance-=amount;
return true;
}
else
{
return false;
}
}
// Creating the sub class derived from Base class Account
class SavingsAccount : public Account
{
private:
// Declaring variables
double rate;
public:
// parameterized constructor
SavingsAccount(string name,int accNo,double balance, double
rate)
: Account(name,accNo,balance)
{
this->rate = rate;
}
// Function declarations
double calculateInterest();
// Function declarations
void deposit(double amount);
bool withdraw(double amount);
};
/* Function implementation
* which calculates the interest amount
*/
double SavingsAccount::calculateInterest()
{
return getBalance() * (rate / 100);
}
/* Function implementation
* which adds money to the current balance
*/
void SavingsAccount::deposit(double amt)
{
setBalance(getBalance() + amt);
cout << "Account Balance after credit :$" <<
getBalance() << endl;
}
/* Function implementation
* which subtract money to the current balance
*/
bool SavingsAccount::withdraw(double amount)
{
if (amount > getBalance())
{
cout << "Debit amount exceeded account balance." <<
endl;
return false;
}
else
{
setBalance(getBalance() - amount);
return true;
}
}
// Creating a Checking Account class derivied from Account
class
class CheckingAccount : public Account
{
private:
public:
// parameterized constructor
CheckingAccount(string name,int accNo,double balance)
: Account(name,accNo,balance)
{
}
// Function declarations
void deposit(double amount);
bool withdraw(double amount);
};
/* Function implementation
* which adds money to the current balance
*/
void CheckingAccount::deposit(double amount)
{
Account::deposit(amount);
}
/* Function implementation
* which subtract money to the current balance
*/
bool CheckingAccount::withdraw(double amount)
{
if (getBalance()-amount<0)
{
cout << "Debit amount exceeded account balance." <<
endl;
return false;
}
else
{
Account::withdraw(amount);
return true;
}
}
int main()
{
//Declaring variables
string name,accType,transType;
int accNo;
double bal,interest,amount;
cout<<"______________________________________________________________"<<endl;
cout<<"Account Name: ";
getline(cin,name);
cout<<"Account Number: ";
cin>>accNo;
cout<<"Account Type: ";
cin>>accType;
cout<<"Current Balance: $ ";
cin>>bal;
cout<<"Transaction Type: ";
cin>>transType;
cout<<"Transaction Amount:$ ";
cin>>amount;
if(accType.compare("Saving")==0)
{
SavingsAccount sa(name,accNo,bal,3);
if(transType.compare("deposit")==0)
{
sa.deposit(amount);
}
else
if(transType.compare("withdrawl")==0)
{
sa.withdraw(amount);
}
cout<<"New Balance:
$"<<sa.getBalance()<<endl;
cout<<"Current interest for
next month: $ "<<sa.calculateInterest()<<endl;
}
else if(accType.compare("Checking")==0)
{
CheckingAccount
ca(name,accNo,bal);
if(transType.compare("deposit")==0)
{
ca.deposit(amount);
}
else
if(transType.compare("withdrawl")==0)
{
ca.withdraw(amount);
}
cout<<"New
Balance: $"<<ca.getBalance()<<endl;
cout<<"Current interest for next month: $
0.0"<<endl;
}
return 0;
}
===================================
Output;
===============================
Output#2:
========================Thank You