Question

In: Computer Science

Suppose that Account class has a method called toString, which will be inherited by Checking and...

Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information.
IT IS C++

Solutions

Expert Solution

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


Related Solutions

Suppose that Account class has a method called toString, which will be inherited by Checking and...
Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information. c++
Animal class Create a simple class called Animal instantiated with a name and a method toString...
Animal class Create a simple class called Animal instantiated with a name and a method toString which returns the name. Cat class Create a simple class Cat which extends Animal, but adds no new instance variable or methods. RedCat class Create a simple class RedCat which extends Cat, but adds no new instance variable or methods. WildCardTester class Create a class with the main method and methods addCat, deleteCat and printAll as follows. addCat method Has two parameters, an ArrayList...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and deposit. Test the methods in the main function.
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use next(), and should not use this._array.forEach! Take note of what should happen if you call forEach twice on the same Iterator instance. The estimated output is commented below In javascript please class Iterator { constructor(arr){ this[Symbol.iterator] = () => this this._array = arr this.index = 0 } next(){ const done = this.index >= this._array.length const value = this._array[this.index++] return {done, value} } //YOUR WORK...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT