Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Suppose you wish to add a friend function for subtraction to the class Money defined in Display \(11.3 .\) What do you need to add to the description of the class Money that we gave in Display \(11.3 ?\) The subtraction function should take two arguments of type Money and return a value of type Money whose value is the value of the first argument minus the value of the second argument.

Short Answer

Expert verified
Answer: To subtract two Money objects using a friend function, you need to first declare the friend function inside the Money class: ```cpp friend Money operator-(const Money& amount1, const Money& amount2); ``` Then, define the friend function outside the Money class: ```cpp Money operator-(const Money& amount1, const Money& amount2) { int all_cents1 = amount1.cents + amount1.dollars*100; int all_cents2 = amount2.cents + amount2.dollars*100; int diff_all_cents = all_cents1 - all_cents2; int final_dollars = diff_all_cents / 100; int final_cents = diff_all_cents % 100; return Money(final_dollars, final_cents); } ``` Now, you can use the '- (minus)' operator to subtract two Money objects, and the friend function will be called, returning the result as a new Money object.

Step by step solution

01

Analyzing the class Money in Display 11.3

First, let's analyze the Money class, which is described in Display 11.3. The class contains a constructor, an output method, and several other functions. We will need to add a friend function for subtraction to this class.
02

Declare the friend function for subtraction within the class Money

Inside the class Money, declare the friend function with the following line: ``` friend Money operator-(const Money& amount1, const Money& amount2); ``` This declares a function named "operator-" that takes two Money objects as constant references and returns a Money object.
03

Define the friend function for subtraction outside the class Money

After the closing brace of the Money class, define the friend function for subtraction as follows: ``` Money operator-(const Money& amount1, const Money& amount2) { int all_cents1 = amount1.cents + amount1.dollars*100; int all_cents2 = amount2.cents + amount2.dollars*100; int diff_all_cents = all_cents1 - all_cents2; int final_dollars = diff_all_cents / 100; int final_cents = diff_all_cents % 100; return Money(final_dollars, final_cents); } ``` This function calculates the total cents for both given Money objects (amount1 and amount2) and then subtracts the total cents of amount2 from the total cents of amount1. The result is converted back into dollars and cents, and a new Money object is created and returned with the resulting values. Now the class Money will have a friend function for subtraction as defined above, and it should work correctly for subtracting two Money objects.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free