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++
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,...
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...
Suppose you deposit $1500 cash into your checking account. By how much will checking deposits in...
Suppose you deposit $1500 cash into your checking account. By how much will checking deposits in the banking system increase as a result when the required reserve ratio is 0.50?
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT