Question: 11. Remember the sterling structure? We saw it in Exercise 10 in Chapter 2, C++ Programming Basics, and in Exercise 11 in Chapter 5, among

11. Remember the sterling structure? We saw it in Exercise 10 in Chapter 2, C++ Programming Basics, and in Exercise 11 in Chapter 5, among other places. Turn it into a class, with pounds (type long), shillings (type int), and pence (type int) data items. Create the following member functions: no-argument constructor one-argument constructor, taking type double (for converting from decimal pounds) three-argument constructor, taking pounds, shillings, and pence getSterling() to get an amount in pounds, shillings, and pence from the user, format 9.19.11 putSterling() to display an amount in pounds, shillings, and pence, format 9.19.11 addition (sterling + sterling) using overloaded + operator subtraction (sterling - sterling) using overloaded - operator multiplication (sterling * double) using overloaded * operator division (sterling / sterling) using overloaded / operator division (sterling / double) using overloaded / operator operator double (to convert to double) To perform arithmetic, you could (for example) add each objects data separately: Add the pence, carry, add the shillings, carry, and so on. However, its easier to use the conversion operator to convert both sterling objects to type double, perform the arithmetic on the doubles, and convert back to sterling. Thus the overloaded + operator looks like this: sterling sterling::operator + (sterling s2) { return sterling( double(sterling(pounds, shillings, pence)) + double(s2) ); } This creates two temporary double variables, one derived from the object of which the function is a member, and one derived from the argument s2. These double variables are then added, and the result is converted back to sterling and returned. Notice that we use a different philosophy with the sterling class than with the bMoney class. With sterling we use conversion operators, thus giving up the ability to catch illegal math operations but gaining simplicity in writing the overloaded math operators.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!