CSE2305 - Object-Oriented Software Engineering
exercises

CSE2305
Object-Oriented Software Engineering

Exercise 4: Rationals

This exercise is designed to reinforce the concepts discussed in the lectures. There are no marks for completing this exercise, however your attendance at tutorials and successful completion is officially recorded and may be used as a positive adjunct to assessment in special circumstances.

You are strongly advised to compete the exercise as it will help you with the concepts discussed in lectures and assist you in successfully completing the practical work. Exercises often mysteriously reappear as exam questions.


Topic: Create a rational number class (this is trickier than it looks...)

Purpose: a chance to gain experience with operator overloading, default constructors, streams, and const objects.

Background: Rational numbers (fractions) are numbers that can be written in the form a/b, where a and b are integers and b != 0. a is known as the numerator and b the denominator. Rational numbers are useful for a number of applications in mathematics and computing.

Your task is to create a Rational class that represents rational numbers and allows rational objects to be created, added, subtracted and printed easily.

Here is a skeletal version of the class to start you off:

class Rational {
 public:
    Rational(int numerator = 0, int denominator = 1); // constructor for 2 ints
    Rational(const Rational& r);
    int getNumerator() const;
    int getDenominator() const;
    double asDouble() const; // returns (double)n/d
    string asString() const; // returns "n/d" as a string
	...
 private:
    int n, d;
};

 

How to Proceed:

  1. Start by providing a full implementation of the class defined above.
  2. Make sure to keep interface and implementation in separate files (".c++" and ".h").
  3. Extend the class so the following functionality is possible:

    Rational a(3), b(2,3), c, d;
    c = a + b;
    cout << a << " + " << b << " = " << c << endl; // should print 3/1 + 2/3 = 11/3
    d = c – b;
    cout << c << " –" << b << " = " << d << endl; // should print 11/3 – 2/3 = 9/3
    a = Rational(1,7); b = Rational(1,9);
    cout << (a + b) << endl; // should print 16/63
    if (a == b) cout << "a is equal to b" << endl;
    if (a < b) cout << "a is less than b" << endl;
    if (a > b) cout << "a is greater than b" << endl;


  4. (Optional) Implement a method gcd (greatest common divisor) so that rational always keeps its representation simplified. e.g.

    Rational r(3,12);
    cout << r << endl; // prints 1/4

Hints/Questions:

  1. You will need to overload operators+, –, ==, !=, <, and > (also <= and >= if you want to be complete).
  2. Make sure you understand the purpose of the const qualifier in method declarations.
  3. Did you test the copy constructor?
  4. For the optional component, you might find this page helpful.


This material is part of the CSE2305 – Object-Oriented Software Engineering course.
Copyright © Jon McCormack, 2005.  All rights reserved.

Last modified: August 11, 2005