Question: Write a rational number class. This problem will be revisited in chapter 11, where operate overloading will make the problem much easier. For now we

Write a rational number class. This problem will be revisited in chapter 11, where operate 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 "ratio-nal" 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. Provide member function input and output that take an istream and ostream argument, respectively, and fetch or write rational numbers in the for 2/3 or 37/51 to or from the keyboard (and to or from a file). 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 funciton neg that has no parameters and returns the negative of the calling object. Provide a main function that thoroughly tests your class implementation. 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.

Teachers Guidelines:

Be sure to include the following members for Rational: a private member variables to hold the numerator and denominator values a default constructor an overloaded constructor that accepts two values for an initial fraction member functions add(), sub(), mul(), div(), less(), eq(), and neq(). a member function to return the current numerator and denominator. a member function that accepts an argument of type ostream that writes the fraction to that open output stream. Do not let either numerator or denominator stored value go negative. Display an error message on the user terminal if any of member functions try to force a negative count value. Test programs will vary. Here is a simple test program for the Rational class: The one member function I would add is a reduce member function. This function should compute the greatest common divisor (GCD) of the numerator and denominator, then divide each by this number. This will reduce the fraction to lowest common terms. If this is not done, the numerator and denominator can grow, possibly far enough to cause integer overflow for int variables.

I have what the text book is asking for, but do not know how to edit to what my instructor is looking for. here is what i have so far:

#include

using namespace std;

class RationalNumber

{

int num, den;

public:

void read()

{

cout<<"Enter Numerator for Rational Number"<

cin>>num;

cout<<"Enter Denominator for Rational Number"<

cin>>den;

}

void add(RationalNumber a, RationalNumber b)

{

num = a.num*b.den + a.den*b.num;

den = a.den*b.den;

hcf();

cout<<"Rational Numbers Addition Completed"<

}

void sub(RationalNumber a, RationalNumber b)

{

num = a.num*b.den - a.den*b.num;

den = a.den*b.den;

hcf();

cout<<"Rational Numbers Subtraction Completed"<

}

void mul(RationalNumber a, RationalNumber b)

{

num = a.num*b.num;

den = a.den*b.den;

hcf();

cout<<"Rational Numbers Multiplication Completed"<

}

void div(RationalNumber a, RationalNumber b)

{

num = a.num*b.den;

den = a.den*b.num;

hcf();

cout<<"Rational Numbers Division Completed"<

}

void lessthan(RationalNumber a, RationalNumber b)

{

num = a.num*b.den;

den = a.den*b.num;

if( num < den )

cout<<"Rational Numbers less than opertion is true between two numbers "<

else

cout<<"Rational Numbers less than opertion is false between two numbers "<

}

int hcf()

{

int a,b,c;

a = num;

b = den;

do

{

c = a%b;

a = b;

b = c;

}

while(c != 0);

num = num/a;

den = den/a;

}

void print()

{

cout<<"Result is : "<

}

};

int main()

{

RationalNumber r1,r2,r3;

r1.read();

r2.read();

r3.add(r1,r2);

r3.print();

r3.sub(r1,r2);

r3.print();

r3.mul(r1,r2);

r3.print();

r3.div(r1,r2);

r3.print();

r3.lessthan(r1,r2);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!