Lab Classes and Operator Overloading

In this lab we will practice the declaration and implementation of member functions, including overloading of arithmetic operators.

Unfortunately, VPL does not offer the ability to separate C++ projects into multiple files. Therefore, for this lab we will work on repl.it. If you haven't already, create an account on repl.it and fork the following project: https://repl.it/@rafaelarceupr/Rational01

Throughout this exercise you should use best practices in member function declaration and implementation, such: passing objects by reference and smart use of const (for parameters and/or the invoking object).

A class for handling rationals

We provide you with files for the declaration (Rational.h) and implementation (Rational.cpp) of a class called Rational that models a number where p and q are integer numbers. We also provide you with a client (client.cpp) that makes use of the Rational class. Your job is to complete the class declaration and implementation using the following the requirements. As you implement each function, use it in the client in a way that clearly shows that it its working.

  1. Implement a reduce() private member function. This function will be used after each arithmetic or setter operation to store the rational in its reduced form. For example, setting the Rational to will actually store . You can use the function __gcd(35, 20) which is available by including the algorithm header file (#include<algorithm>) as shown in the main function.
  2. Overload the ! unary operator to return the reciprocal of a rational. For example, if a is , then !a will return .
  3. Overload the < operator. For example, if a is and b is , then a < b should return false.

Implementing a simple list of Rationals

Lists are a very common and useful data structure that can be used to store a collection of elements of the same type and access them through their indices, sort of like the array but with added functionality. In this exercise we will create a class that can contain multiple Rational objects. We will call it the RationalList class, although as you will later see in class that lists offer many other member functions than the ones we will give RationalList. For the sake of convenience we will create the declaration and implementation in the Rational.h and RationalList.cpp files, respectively.

We would like each object of the RationalList class to be able to store as much as 10 frac objects, organized into an array. The data member part of the fracList class is as follows:

class RationalList {
private:
  Rational a[10];
  int size;
};

The size variable keeps track of how many fractions have been stored in the array. As elements are added or deleted the variable size must increased or decreased accordingly. Create a default constructor for the class fracList. It should initialize the size to 0, to establish that a fracList contains 0 fractions when it is initialized.

  1. Create a member function push(int n, int d) that concatenates the Rational to the end of a RationalList. For example, if the list currently contains 0 Rationals (you can tell this by looking at the size) you would add the fraction at index 0. The push() function should determine if the size is less than 10 before trying to insert a Rational (since the list can contain at most 10 rationals!).

  2. Create a member function calles void display(ostream &out) that when passed cout as parameter, prints all the rationals in the list to the terminal.

  3. Once you have display(ostream &out) working, overload the operator<< so that if L is a RationalList, a command like this will work cout << L;

  4. Create a member function sum() for the class RationalList. It should sum all the fractions in the list and return the reduced result. For example, if the fracList contains the three fractions: , , , the result is .

The following is an example client (extract) and its expected results:

RationalList L;
L.push(1,2);
L.push(3,4);
L.push(1,5);

cout << "The contents of L: " << L << endl;

cout << "Their sum: " << L.sum() << endl;
The contents of L: 1/2 3/4 1/5
Their sum: 29/20