Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Question
Book Icon
Chapter 16, Problem 1P
Program Plan Intro

  • Include required library files.
  • Define a class named “NegativeAmount” and “InsufficientFunds”.
  • Inside the “Account” class,
    • Add the negative amount exception in the “deposit()” function.
    • Add the negative amount and insufficient funds exception in the “withdraw()” function.
  • Define a “main()” function to test the program to deposit and withdraw amounts.
    • Create an object and test the deposit and withdraw using the appropriate function.
    • Then print the result.

Expert Solution & Answer
Check Mark
Program Description Answer

Program to rewrite the given class “Account” that throws an appropriate exceptions and test the program to deposit and withdraw amounts are as follows,

Explanation of Solution

// Include required library files

#include <iostream>

using namespace std;

// Definition of class NegativeAmount

class NegativeAmount

{

};

// Definition of class insufficientFunds

class InsufficientFunds

{

};

// Definition of class Account

class Account

{

  // Access specifier

  private:

    // Declaration of double variable

    double balance;

  // Access specifier

  public:

    // Definition of default constructor

    Account()

    {

      // Assign 0 to balance

      balance = 0;

    }

    // Definition of constructor to assign value

    Account(double initialDeposit)

    {

      // Assign initialDeposit to balance

      balance = initialDeposit;

    }

  // Definition of getBalance() function to return balance

    double getBalance()

    {

      // Return balance

      return balance;

    }

  // Definition of deposit() function to return new balance

  // or throw NegativeAmount exception

  double deposit(double amount) throw (NegativeAmount)

  {

    // Check amount is greater than 0

    if (amount > 0)

      // Condition is trur, compute balance

      balance += amount;

    // Otherwise

    else

      // Throw exception

      throw NegativeAmount();

    // Return balance

    return balance;

  }

  // Definition of withdraw function to return new balance

  // or throw NegativeAmount or InsufficientFunds

  double withdraw(double amount) throw (NegativeAmount,

  InsufficientFunds)

  {

    // Check amount is greater than balance

    if (amount > balance)

      // Throw insufficient exception

      throw InsufficientFunds();

    // Otherwise check amount is lesser than 0

    else if (amount < 0)

      // Throw negative amount execption

      throw NegativeAmount();

    // Otherwise

    else

      // Compute balance

      balance -= amount;

    // Return balance

    return balance;

  }

};

// Definition of main() function

int main()

{

  // Creation of object and passing 100 to the constructor

  Account a(100);

  // Try block

  try

  {

    // Call deposit() function to deposit 50

    a.deposit(50);

    // Call deposit() function to deposit 10

    a.deposit(10);

    // Call withdraw() function to deposit 220

    a.withdraw(220);

  // Call getBalance() function to get print balance amount

    cout << a.getBalance() << endl;

  }

  // Catch block for negative amount

  catch (NegativeAmount)

  {

    // Print the message regarding negative amount

      cout << "You can't deposit or withdraw a negative amount."

    << endl;

  }

  // Catch block for insufficient funds

  catch (InsufficientFunds)

  {

    // Print the message for insufficient funds

    cout << "Insufficient funds for withdrawal." << endl;

  }

  // Declaration of character variable

  char ch;

  // Get an character

  cin >> ch;

  // Return 0

  return 0;

}

Sample Output

Output:

Insufficient funds for withdrawal.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
C++ Visual Studio 2019  Complete #13. Dependent  #1 Employee and ProductionWorker classes showing below. Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. The ProductionWorker class should throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate. Write a driver program that demonstrates how each of these exception conditions works.  #1 Employee and ProductionWorker classes #include <string>#include <iostream>#include <iomanip>using namespace std; class Employee{private:    string name;        // Employee name    string number;        // Employee number    string hireDate;    // Hire date public:    // Default…
The following class maintains an account balance and returns aspecial error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to depositinvalid amounts and catches the exceptions that are thrown
Cpp program Write a Garage class that has a Car that is having troubles with its Motor. Use a function-level try block in the Garage class constructor to catch an exception (thrown from the Motor class) when its Car object is initialized. Throw a different exception from the body of the Garage constructor s handler and catch it in main().
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage