11.8. Practice Problems / Worked Examples

Review

The textbook frequently reiterates two fundamental themes: the value of self-testing and the necessity of elaborating on what we study.

This section revisits two previous programs and asks you to modify them. Each program consists of a single class and a simple driver. Your task is converting some member functions to overloaded operators and changing the corresponding function calls in the driver to use the operators. The following sub-sections outline the required conversions, and the complete worked examples follow.

Time

Time is a good class example because it's a relatively simple and familiar concept, having a modest number of attributes and operations. The most challenging operations, converting between an instance of Time and a single integer representing seconds, were completed in the previous examples. The following figure illustrates the next step in the evolution of the Time problem.

Chapter 9 Class Version   Overloaded Operator Version
The Time UML class diagram:
Time
-----------------
-hours : int
-minutes : int
-seconds : int
-----------------
+Time()
+Time(h : int, m : int, s : int)
+Time(s : int)
+add(t2 : Time) : Time
+print() : void
+read() : void Right-pointing arrow UML class diagram for the Time class:
Time
----------------------
-hours : int = 0
-minutes : int = 0
-seconds : int = 0
----------------------
+Time()
+Time(h : int, m : int, s : int)
+Time(s : int)
+operator+(t2 : Time) : Time
+operator+(i : int) : Time
+operator<<(out : ostream&, t : Time&) : ostream&
+operator>>(in : istream&, t : Time&) : istream&
Overloaded operators problem with the Time class. Convert the add and I/O functions in the previous Time Example to overloaded operators, and update the driver to use the operators instead of the functions.

Version 2: Replace both operator+ functions with a single friend function supporting Time + Time, Time + int, and int + Time (A single friend overloaded operator+)

Compare your solution to the worked example in the next section.

fraction

The fraction class's arithmetic operations are more complex than Time's, but the Chapter 9 fraction example solves them completely.

Chapter 9 Class Version   Overloaded Operator Version
A UML class diagram for the fraction class. The diagram contains the following information:
fraction
----------------------------
-numerator : int
-denominator: int
----------------------------
+fraction(n : int = 0, d : int = 1)
+add(f : fraction) : fraction
+sub(f : fraction) : fraction
+mult(f : fraction) : fraction
+div(f : fraction) : fraction
+print() : void
+read() : void Right-pointing arrow The fraction UML class diagram with overloaded operators. The class has two private integer members, a numerator and a denominator. Its constructor is unchanged from the original version. However, this version replaces the add, sub, mult, and div functions with the corresponding overloaded operators: +, -, *, and / implemented as friends. The operator version also replaces the print and read functions with an inserter and extractor.
Overloaded operators problem with the fraction class. Convert the arithmetic (add, sub, mult, and div) and and I/O (print and read) functions in the original version to overloaded operators. The number of parameters in the arithmetic operators indicates they are friend functions. Modify the driver to use the operators. Carefully compare your solution to the worked example following the Time solution.