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.
- 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 themain
function. - Overload the
!
unary operator to return the reciprocal of a rational. For example, ifa
is , then!a
will return . - Overload the < operator. For example, if
a
is andb
is , thena < b
should returnfalse
.
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.
-
Create a member function
push(int n, int d)
that concatenates the Rational to the end of aRationalList
. 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. Thepush()
function should determine if thesize
is less than 10 before trying to insert a Rational (since the list can contain at most 10 rationals!). -
Create a member function calles
void display(ostream &out)
that when passedcout
as parameter, prints all the rationals in the list to the terminal. -
Once you have
display(ostream &out)
working, overload theoperator<<
so that ifL
is a RationalList, a command like this will workcout << L;
-
Create a member function
sum()
for the classRationalList
. 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