
Concept explainers
Modify the definition of the class Money shown in Display 11.8 so that all of the following are added:
a. The operators <, <=, >, and >= have each been overloaded to apply to the type Money. (Hint: See Self-Test Exercise 13.)
b. The following member function has been added to the class definition. (We show the function declaration as it should appear in the class definition. The definition of the function itself will include the qualifier Money::.)
Money percent(int percentFigure) const; //Returns a percentage of the money amount in the //calling object. For example, if percentFigure is 10, //then the value returned is 10% of the amount of //money represented by the calling object. |
For example, if purse is an object of type Money whose value represents the amount $100.10, then the call
purse.percent(10); |
returns 10% of $100.10; that is, it returns a value of type Money that represents the amount $10.01.

“Money” Class
Program plan:
- Include required header file.
- Define a class for “Money”.
- Declare the function for overload operator “<”, “<=”, “>’, “>=”, “+”, “-” and “==”.
- Declare the constructor for “Money” class.
- Declare the function for compute total amount, total dollars and total cents.
- Declare the function for “percent” function.
- Declare the function for operator “>>” and “<<”.
- Declare required variables.
- Define function for overload operator “<”, “<=”, “>” and “>=”.
- Define function for “percent”.
- This function is used to compute the percentage amount for given money.
- Define function for arithmetic operator “+” and “-” with two arguments.
- Define function for overload operator “==”.
- Define function for overload operator “-” with one parameter.
- Define default constructor for “Money” class, constructor with one argument and two arguments.
- Define the function “computeTotalAmount”.
- This function is used to compute the total amount.
- Define the function “computeTotalDollars”.
- This function is used to compute the dollars in given amount.
- Define the function “computeTotalCents”.
- This function is used to compute the cents in given amount.
- Define the function “forDollarConversion” which is used to convert amount to dollar
- Define the function “forCentsConversion” which is used to convert the amount to cents.
- Define the function “forRoundedValue” which is used for convert the result in rounded value.
- Define function for input and output operator.
- Define main function
- Create an object for “Money” class.
- Create an object for “Money” class with argument.
- Declare variable for file input and file output.
- Open the given input file.
- Check the given file is found or not using “if” loop.
- Read the amount from file.
- Display the purse amount.
- Compare the given amount and purse amount.
- Compute sum of amount and purse and display it.
- Compute the difference of two amount and display it.
- Check the amount using the comparison operator “>=”.
- Check the amount using the comparison operator “>”.
- Check the amount using the comparison operator “<=”.
- Check the amount using the comparison operator “<”.
- Finally close input and output file.
The below C++ program is used to implement the “Money” class with the overload operators such as “<”, “<=”, “>” and “>=” and compute the percent of given amount.
Explanation of Solution
Program:
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cmath>
using namespace std;
//Create a class name, Money
class Money
{
public:
//Function declaration for operators <, <=, >, and >=
friend bool operator < (const Money& amount1, const Money& amount2);
friend bool operator <= (const Money& amount1, const Money& amount2);
friend bool operator > (const Money& amount1, const Money& amount2);
friend bool operator >= (const Money& amount1, const Money& amount2);
//Function declaration for operators +, -, and ==
friend Money operator + (const Money& amount1, const Money& amount2);
friend Money operator - (const Money& amount1, const Money& amount2);
friend Money operator - (const Money& amount);
friend bool operator == (const Money& amount1, const Money& amount2);Money();
//Constructor for "Money" class
Money(double amount);
Money(int dollars, int cents);
Money(int nDollars);
/* Function declaration for compute total amount, total dollars and total cents */
double computeTotalAmount() const;
int computeTotalDollars() const;
int computeTotalCents() const;
/* Function declaration for compute the percent for given amount */
Money percent(int percentFigure) const;
friend istream& operator >>(istream& ins, Money& amount);
//Overloads the >> operator so it can be used to input values of type Money.
//Notation for inputting negative amounts is as in -$100.00.
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file.
friend ostream& operator <<(ostream& outs, const Money& amount);
//Overloads the << operator so it can be used to output values of type Money.
//Precedes each output value of type Money with a dollar sign.
//Precondition: If outs is a file output stream,
//then outs has already been connected to a file.
private:
//Declare member variables
int dollars, cents;
int forDollarConversion(double amount) const;
int forCentsConversion(double amount) const;
int forRoundedValue(double number) const;
};
//Function definition of operator '<'
bool operator < (const Money& amount1, const Money& amount2)
{
//Return given result
return (amount1.dollars < amount2.dollars) ||
((amount1.dollars == amount2.dollars) &&
(amount1.cents < amount2.cents));
}
//Function definition of operator '<='
bool operator <= (const Money& amount1, const Money& amount2)
{
//Return given result
return ((amount1.dollars < amount2.dollars) || ((amount1.dollars == amount2.dollars) &&
(amount1.cents < amount2.cents)))
|| ((amount1.dollars == amount2.dollars)
&& (amount1.cents == amount2.cents));
}
//Function definition of operator '>'
bool operator > (const Money& amount1, const Money& amount2)
{
//Return given result
return (amount1.dollars > amount2.dollars)
|| ((amount1.dollars == amount2.dollars) && (amount1.cents > amount2.cents));
}
//Function definition of operator '>='
bool operator >=(const Money& amount1, const Money& amount2)
{
//Return given result
return ((amount1.dollars > amount2.dollars) ||
((amount1.dollars == amount2.dollars) &&
(amount1.cents > amount2.cents))) ||
((amount1.dollars == amount2.dollars) &&
(amount1.cents == amount2.cents));
}
//Function definition of percent
Money Money::percent(int percentFigure) const
{
int d = dollars * percentFigure / 100;
int c = dollars * percentFigure % 100 + cents * percentFigure / 100;
return Money(d, c);
}
//Function definition of operator '+'
Money operator + (const Money& amount1, const Money& amount2)
{
int cent1 = amount1.cents + amount1.dollars * 100;
int cent2 = amount2.cents + amount2.dollars * 100;
int totalCents = (cent1 + cent2);
int aCents = abs(totalCents);
int tDollars = aCents / 100;
int tCents = aCents % 100;
if (totalCents < 0)
{
tDollars = -tDollars;
tCents = -tCents;
}
//Return given result
return Money(tDollars, tCents);
}
//Function definition of operator '-' for two arguments
Money operator -(const Money& amount1, const Money& amount2)
{
int c1 = amount1.cents + amount1.dollars * 100;
int c2 = amount2.cents + amount2.dollars * 100;
int differCents = c1 - c2;
int aCents = abs(differCents);
int tDollars = aCents / 100;
int tCents = aCents % 100;
if (differCents < 0)
{
tDollars = -tDollars;
tCents = -tCents;
}
//Return given result
return Money(tDollars, tCents);
}
//Function definition of operator '=='
bool operator == (const Money& amount1, const Money& amount2)
{
//Return given result
return((amount1.dollars == amount2.dollars) && (amount1.cents == amount2.cents));
}
//Function definition of operator '-' for one argument
Money operator - (const Money& amount)
{
//Return given result
return Money(-amount.dollars, -amount.cents);
}
//Constructor initialization
Money::Money(): dollars(0), cents(0)
{
}
//Constructor for "Money" class
Money::Money(double amount): dollars(forDollarConversion(amount)), cents(forCentsConversion(amount))
{
}
//Constructor for "Money" class
Money::Money(int d): dollars(d), cents(0)
{
}
//Constructor for "Money" class
Money::Money(int d, int c)
{
if ((d < 0 && c > 0) || (d > 0 && c < 0))
{
cout << "The given data is invalid" << endl;
exit(1);
}
dollars = d;
cents = c;
}
//Function definition of computeTotalAmount
double Money::computeTotalAmount() const
{
//Return given result
return (dollars + cents*0.01);
}
//Function definition of computeTotalDollars
int Money::computeTotalDollars() const
{
//Return given result
return dollars;
}
//Function definition of computeTotalCents
int Money::computeTotalCents() const
{
//Return given result
return cents;
}
//Function definition of forDollarConversion
int Money::forDollarConversion(double amount) const
{
//Return given result
return static_cast<int>(amount);
}
//Function definition of forCentsConversion
int Money::forCentsConversion(double amount) const
{
/* Compute cents from given amount */
double doubleCents = amount * 100;
int ncents = (forRoundedValue(fabs(doubleCents))) % 100;
if (amount < 0)
ncents = -ncents;
return ncents;
}
//Function definition of forRoundedValue
int Money::forRoundedValue(double value) const
{
//Return given result
return floor(value + 0.5);
}
//Function definition for "<<" operator
ostream& operator <<(ostream& outs, const Money& amount)
{
/* Compute dollars and cents using "abs" */
int aDollars = abs(amount.dollars);
int aCents = abs(amount.cents);
//Check condition for cents
if (amount.dollars < 0 || amount.cents < 0)
outs << "$-";
else
outs << '$';
outs << aDollars;
if (aCents >= 10)
outs << '.' << aCents;
else
outs << '.' << '0' << aCents;
return outs;
}
//Function definition for operator '>>'
istream& operator >> (istream& ins, Money& amount)
{
//Declare variables
char dollarSign;
double increaseAmount;
//Read sign of dollars from file
ins >> dollarSign;
/* Check the dollar sign */
if (dollarSign != '$')
{
/* Display message */
cout << "No dollar symbol" << endl;
exit(1);
}
//Read increase amount from file
ins >> increaseAmount;
//Compute dollars for given amount
amount.dollars = amount.forDollarConversion(increaseAmount);
//Compute cents for given amount
amount.cents = amount.forCentsConversion(increaseAmount);
return ins;
}
//Main function
int main()
{
//Create a object for "Money" class
Money amount;
//Create a object for "Money" class with argument
Money purse(100, 10);
//Declare variable for file input
ifstream inStream;
//Declare variable for file output
ofstream outStream;
//Open input file
inStream.open("infile.dat");
/* If the given file is not found, then */
if (inStream.fail())
{
//Display an error message
cout << "Input file opening failed.\n";
//For exit function
exit(1);
}
//Open the output file
outStream.open("outfile.dat");
/* If the output file is not found, then */
if (outStream.fail())
{
//Display an error message
cout << "Output file opening failed.\n";
//For exit function
exit(1);
}
//Read the amount from file
inStream >> amount;
//Display given statement to output file
outStream << amount << " copied from the file infile.dat.\n";
//Display the output
cout << amount << " copied from the file infile.dat.\n";
cout <<"The purse amount is " << purse << endl;
//Call percent function for purse amount
cout << "10% of purse amount: " << purse.percent(10) << endl;
//Compare the given amout and
if (amount == purse)
cout << "The given amount and purse amounts are equal." << endl;
else
cout << "The given amount and purse amounts are not equal." << endl;
//Compute sum of amount and purse
Money totalAmount = amount + purse;
//Display sum of amount
cout << amount << " + " << purse << " = " << totalAmount << endl;
//Compute the difference of two amount
Money diff = amount - purse;
/* Display difference of two amount */
cout << amount << " - " << purse << " = " << diff << endl;
/* Check the amount using the comparison operator ">=" */
if (amount >= purse)
/* Display message */
cout << "The amount is greater than or equal to the purse amount. " << endl;
/* Check the amount using the comparison operator ">" */
if (totalAmount > diff)
/* Display message */
cout << "The sum of amounts is greater than the difference amount. " << endl;
/* Check the amount using the comparison operator "<=" */
if (amount <= purse)
/* Display message */
cout << "The given amount is less than or equal to purse amount. " << endl;
else
/* Display message */
cout << "The given amount is greater than the purse amount. " << endl;
/* Check the amount using the comparison operator "<" */
if (totalAmount < diff)
/* Display message */
cout << "The sum of amounts is less than the difference." << endl;
else
/* Display message */
cout << "The sum of amounts is greater than the difference." << endl;
/* Close input file*/
inStream.close();
/* Close output file */
outStream.close();
}
infile.dat:
$1.11
$2.22
$3.33
$1.11 copied from the file infile.dat.
The purse amount is $100.10
10% of purse amount: $10.01
The given amount and purse amounts are not equal.
$1.11 + $100.10 = $101.21
$1.11 - $100.10 = $-98.99
The sum of amounts is greater than the difference amount.
The given amount is less than or equal to purse amount.
The sum of amounts is greater than the difference.
outfile.dat:
$1.11 copied from the file infile.dat.
Want to see more full solutions like this?
Chapter 11 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Degarmo's Materials And Processes In Manufacturing
SURVEY OF OPERATING SYSTEMS
Starting Out with Python (4th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out With Visual Basic (8th Edition)
- Using R language. Here is the information link. http://www.cnachtsheim-text.csom.umn.edu/Kutner/Chapter%20%206%20Data%20Sets/CH06PR18.txtarrow_forwardUsing R languagearrow_forwardHow can I type the Java OOP code by using JOptionPane with this following code below: public static void sellCruiseTicket(Cruise[] allCruises) { //Type the code here }arrow_forward
- Draw a system/level-0 diagram for this scenario: You are developing a new customer relationship management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary. Example of what a level-0 diagram looks like is attached.arrow_forwardWhat is the value of performing exploratory data analysis in designing data visualizations? What are some examples?arrow_forwardDraw a level-0 diagram for this scenario: You are developing a new customer relationship management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary.arrow_forward
- Draw a context diagram for this scenario: You are developing a new customer relationship Management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in. the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary.arrow_forwardWrite a complete Java program named FindSumAndAverage that performs the following tasks in 2-D array: Main Method: a. The main() method asks the user to provide the dimension n for a square matrix. A square matrix has an equal number of rows and columns. b. The main() method receives the value of n and calls the matrixSetUp() method that creates a square matrix of size n and populates it randomly with integers between 1 and 9. c. The main method then calls another method named printMatrix() to display the matrix in a matrix format. d. The main method also calls a method named findSumAndAverage() which: • Receives the generated matrix as input. • Calculates the sum of all elements in the matrix. • Calculates the average value of the elements in the matrix. • Stores these values (sum and average) in a single-dimensional array and returns this array • e. The main method prints the sum and average based on the result returned from findSumAndAverage()). Enter the dimension n for the square…arrow_forwardThe partial sums remain the same no matter what indexing we done to s artial sum of each series onverges, * + s of each series to the series or show 12. (1)+(0)+(0)+(+1)+ 17, " (F) + (F) + (F)(F)(- 18. 19. 1 #20. (三)+(三)-(三)+(3) 20 (9)-(0)-(0)-- 10 +1 2.1+(男)+(男)+(罰)+(鄂 9 T29 x222-끝+1-23 + -.... Repeating Decimals 64 Express each of the numbers in Exercises 23-30 as the m integers. 23. 0.23 = 0.23 23 23... 24. 0.234 = 0.234 234 234. 25. 0.7 = 0.7777... 26. 0.d = 0.dddd... where d is a digit natio of own s converges or * 27. 0.06 = 0.06666.. 28. 1.4141.414 414 414... 29. 1.24123 = 1.24 123 123 123... 30. 3.142857 = 3.142857 142857. Using the ath-Term Test In Exercises 31-38, use the ath-Term Test for divergence to show that the series is divergent, or state that the test is inconclusive 8arrow_forward
- CPS 2231 Computer Programming Homework #3 Due Date: Posted on Canvas 1. Provide answers to the following Check Point Questions from our textbook (5 points): a. How do you define a class? How do you define a class in Eclipse? b. How do you declare an object's reference variable (Hint: object's reference variable is the name of that object)? c. How do you create an object? d. What are the differences between constructors and regular methods? e. Explain why we need classes and objects in Java programming. 2. Write the Account class. The UML diagram of the class is represented below (10 points): Account id: int = 0 - balance: double = 0 - annualInterestRate: double = 0.02 - dateCreated: java.util.Date + Account() + Account(id: int, balance: double) + getId(): int + setId(newId: int): void + getBalance(): double + setBalance(newBalance: double): void + getAnnualInterestRate(): double + setAnnualInterest Rate (newRate: double): void + toString(): String + getDataCreated(): java.util.Date +…arrow_forwardTHIS IS NOT A GRADING ASSIGNMENT: Please only do lab 2.2 (bottom part of the first picture) For that Lab 2.2 do: *Part 1 (do the CODE, that's super important I need it) *Part 2 *Part 3 I also attached Section 2.5.2 which is part of the step 1 so you can read what is it about. Thank you!arrow_forwardTHIS IS NOT A GRADING ASSIGNMENT: Please only do lab 2.2 (bottom part of the first picture) For that Lab 2.2 do: *Part 1 *Part 2 *Part 3 I also attached Section 2.5.2 which is part of the step 1 so you can read what is it about. Thank you!arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ 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,Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage



