- 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.
- Add the negative amount exception in the “deposit()” function.
- Add the negative amount and insufficient funds exception in the “withdraw()” function.
- Create an object and test the deposit and withdraw using the appropriate function.
- Then print the result.
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;
}
Output:
Insufficient funds for withdrawal.
Want to see more full solutions like this?
Chapter 16 Solutions
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
- can u solve this questionarrow_forward1. Unsigned Integers If we have an n-digit unsigned numeral dn-1d n-2...do in radix (or base) r, then the value of that numeral is n−1 r² di Σi=0 which is basically saying that instead of a 10's or 100's place we have an r's or r²'s place. For binary, decimal, and hex r equals 2, 10, and 16, respectively. Just a reminder that in order to write down a large number, we typically use the IEC or SI prefixing system: IEC: Ki = 210, Mi = 220, Gi = 230, Ti = 240, Pi = 250, Ei = 260, Zi = 270, Yi = 280; SI: K=103, M = 106, G = 109, T = 10¹², P = 1015, E = 10¹8, Z = 1021, Y = 1024. 1.1 Conversions a. (15 pts) Write the following using IEC prefixes: 213, 223, 251, 272, 226, 244 21323 Ki8 Ki 223 23 Mi 8 Mi b. (15 pts) Write the following using SI prefixes: 107, 10¹7, 10¹¹, 1022, 1026, 1015 107 10¹ M = 10 M = 1017102 P = 100 P c. (10 pts) Write the following with powers of 10: 7 K, 100 E, 21 G 7 K = 7*10³arrow_forwardanswer shoul avoid using AI and should be basic and please explainarrow_forward
- Node A is connected to node B by a 2000km fiber link having a bandwidth of 100Mbps. What is the total latency time (transmit + propagation) required to transmit a 4000 byte file using packets that include 1000 Bytes of data plus 40 Bytes of header.arrow_forwardanswer should avoid using AI and should be basic and explain pleasearrow_forwardasnwer should avoid using AIarrow_forward
- answer should avoid using AI (such as ChatGPT), do not any answer directly copied from AI would and explain codearrow_forwardWrite a c++ program that will count from 1 to 10 by 1. The default output should be: 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10 There should be only a newline after the last number. Each number except the last should be followed by a comma and a space. To make your program more functional, you should parse command line arguments and change behavior based on their values. Argument Parameter Action -f, --first yes, an integer Change place you start counting -l, --last yes, an integer Change place you end counting -s, --skip optional, an integer, 1 if not specified Change the amount you add to the counter each iteration -h, —help none Print a help message including these instructions. -j, --joke none Tell a number based joke. So, if your program is called counter, counter -f 10 --last 4 --skip 2 should produce 10, 8, 6, 4 Please use the last supplied argument. If your code is called counter, counter -f 4 -f 5 -f 6 should count from 6. You should…arrow_forwardshow workarrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning