Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
9th Edition
ISBN: 9780133862218
Author: Walter Savitch
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
in C# i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a Book does not meet the price-to-pages ratio. For example, an error message: For Goodnight Moon, ratio is invalid. ...Price is $12.99 for 25 pages. Next, create a Book class that contains fields for title, author, price, and number of pages. Include properties for each field. Throw a BookException if a client program tries to construct a Book object for which the price is more than 10 cents per page. Finally, using the Book class, create an array of five Books. Prompt the user for values for each Book. To handle any exceptions that are thrown because of improper or invalid data entered by the user, set the Book’s…
In C++  You have a class called Fraction, which uses the heap. The constructor for the class is declared as: Fraction::Fraction(); Implement an exception handling design in the parameterized constructor to catch zeros in the denominator. Show the new exception class you will need to put in the header (specification) file. Show the definition for your parameterized constructor that throws the exception if the denominator is zero. Finally, show how to use the try-catch statement in the driver program when instatiating a new paramerized Fraction object. You do not need to show entire programs, only the requested lines of source code
Write a C++ program to check for the not-eligible donor by throwing a custom exception.Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, member variable names, and function names should be the same as specified in the problem statement. The class Donor has the following protected variables.  Data Type Variable string name int age int weight string bloodGroup   In the Donor class, define the following function. Method Description bool validateDonor(Donor donor) This method validates donor eligibility. Validate the donor details with the following conditions. 1.Age must be greater than 30 then the donor is eligible. 2.If the age is less than 30 then throw an exception and check the weight should be greater than 44 kg in catch block, If the weight is also less than 44 then the donor is not eligible and rethrow the exception to the main method. If the weight is greater than 44 then the donor is eligible.   In the…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education