Tags: %TAGME{ tpaction="" web="KenyonCpp" tag="" }% view all tags

Lab 9

Due: Nov 20, 11:55pm


  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
  • Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.


Feature %
Program correctness and completeness with respect to defination 70%
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in the run the requested inputs below.. 10%


Write a rational number class. This problem will be revisited in Chapter 11, where operator overloading will make the problem much easier. For now we will use member functions add, sub, mul, div, and less that each carry out the operations +, -, *, /, and <. For example, a + b will be written a.add(b), and a < b will be written a.less(b).

Define a class for rational numbers. A rational number is a “rational” number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32, 65/4, 16/5. You should represent rational numbers by two int values, numerator and denominator.

A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; this is a constructor with two int parameters. Since every int is also a rational number, as in 2/1 or 17/1, you should provide a constructor with a single int parameter.

(Optional to use files, worth extra credit) Provide member functions input and output that take an istream and ostream argument, respectively, and fetch or write rational numbers in the form 2/3 or 37/51 to or from the keyboard (and to or from a file). Ask the users to enter a filename if they want the program to read/write from a file. If they don't provide a filename, use cin and cout.

Provide member functions add, sub, mul, and div that return a rational value. Provide a function less that returns a bool value. These functions should do the operation suggested by the name. Provide a member function neg that has no parameters and returns the negative of the calling object. Provide a main function that uses your class implementation as seens below. The following formulas will be useful in defining functions.

  • a/b + c/d = (a*d + b*c) / (b*d)
  • a/b - c/d = (a*d - b*c) / (b*d)
  • (a/b) * (c/d) = (a*c) / (b*d)
  • (a/b) / (c/d) = (a*d) / (c*b)
  • -(a/b) = (-a/b)
  • (a/b) < (c/d) means (a*d) < (c*b)
  • (a/b) = (c/d) means (a*d) = (c*b)
Let any sign be carried by the numerator; keep the denominator positive.

Class member functions

You will need at least the following class methods for the class Rational:

  • Rational(); // Default constructor
  • Rational(int x, int y); // constructor
  • Rational(int x); // Constructor of x/1
  • Rational add(Rational x);
  • Rational minus(Rational x);
  • Rational iimes(Rational x);
  • Rational divide(Rational x);
  • bool less(Rational x);
  • bool equals(Rational x);
  • void input(ifstream s); // Inputs into this rational
  • void output(x, ofstream s); // Outputs this rational
  • int getNum(); // Get the numerator
  • int getDem(); // Get denominator

Program Operation

Write a main line that asks for two rational numbers to be typed in, and does all of the operations, just as seen in the example below:

Please enter a rational number of the form a/b: 4/5
And another, please: 7/9
The two numbers you input are (in lowest terms) 4/5 and 7/9.

The negatives of your two numbers are -4/5 and -7/9.
The sum of your two numbers is 71/45.
The difference of your two numbers is 1/45.
The product of your two numbers is 28/45.
The quotient of your two numbers is 36/35.
Furthermore, 7/9 is less than 4/5.

Thanks for using this program.

Turn in

  1. Solution to the problem above
  2. Solution to 5/11 and 9/13
  3. Solution to -12/7 and 16/7
Edit | Attach | Watch | Print version | History: r6 < r5 < r4 < r3 < r2 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r6 - 2015-11-19 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback