## Computing Concepts with C++ Essentials, 3rd ed. Laboratory Notebook Chapter 17 - Advanced C++ Topics

### John P. Russo Cay S. Horstmann Geof Pawlicki

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

#### Lab Objectives

To gain experience with

• automatic memory management
• implementing templates
• exception handling

Implement a class Fraction with a constructor

`Fraction(long numerator, long denominator)`

and overloaded operators + - * / << to add, subtract, multiply and divide fraction objects. Then test your program to compute the expression "1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10".

You must keep track of the numerator and denominator separately as exact values. Do not use double. This way, you will obtain the numerator and denominator of the result precisely, without roundoff error.

For example, to add two fractions, you use the formula

a/b + c/d = (ad + bc)/(bd)

and then reduce to the lowest common terms, by dividing through the greatest common divisor.

```Fraction Fraction::operator+(Fraction b)
{   long n = num * b.den + den * b.num;
long d = den * b.den;
long g = gcd(n, d);
return Fraction(n / g, d / g);
}```

Here is a "greatest common divisor" function for your use:

```int gcd(int a, int b)
// compute the greatest common divisor
{  if (a < 0) return gcd(-a, b);
if (a == 0) return b;
if (b < a)  return gcd(b, a);
// now 0 < a <= b
return gcd(b % a, a);
// b % a is the remainder of the division b / a
}
```

### P2. Automatic Memory Management

In order to effectively manage assignment of heap memory, you must use the "big three": destructor, copy constructor and overloading of the assignment operator. Your text discusses this on pages 610 through 622. Modify the Student_Club class that you created in the chapter 10 lab in order to automatically manage memory using the "big three".

### P3. Templates

Templates let you design a family of related classes that differ by the type of some of the data members or member functions. In this exercise, you will design a template for a table of items of an arbitrary type. For example,

`Table<double> balances(12, 5);`

make a table of items of type double, with 12 rows and 5 columns.

Supply two member functions:

```value = balances.get(row, col);
balances.set(row, col, value);```

Hint: As implementation, use a vector with nrows * ncols elements. To access the element in row i and column j, access v[i * ncols + j].

Your job is now to design a Table class template and to put it to use by re-implementing the matrix.cpp program in your textbook on p. 358, using a Table<double> instead of a two-dimensional C-style array.

### P4. Exception Handling

Recall that in the Chapter 16 lab exercise P4 you created a map container for objects of class Student, using the social security number as the index. Enhance your program by adding an exception handler to deal with the situation where the user enters a social security number that is not in the map or a social security number that is non-numeric. What exception type would you use for this?