Question: Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2,

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int one for the numerator and one for the denominator. Call the class Rational.

Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also, include a constructor that has only a single parameter of type int; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. Also include a default constructor that initializes an object to 0 (that is, to 0/1).

Overload the input and output operators >> and , >=,+,-,*,/. Also write a test program to test your class. Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*d You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as -1/2. You should also write a test program to test your class. using this template

Define a class for rational numbers. A rational number is a numberthat can be represented as the quotient of two integers. For example,1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2,etc., we mean the everyday meaning of the fraction, not the integerdivision this expression would produce in a C++ program.) Represent rational numbers

1/class Rational; 1/private data: int n, (fraction numerator) and int d (fraction denominator). //public interface: //constructors: //two int args, to allow setting rational to any legal value 1/one int arg, to construct rationals with arg numerator and denominator 1. 1/overload > to allow writing to screen in form 325/430 1/and reading it from the keyboard in the same format. //Notes: either n or d may contain a negative quantity. 1/overload + -/ >= 1/Put definitions in separate ?ile for separate compilation #include #include using namespace std; class Rational { public: Rational (int numerator, int denominator); Rational (int numerator); // sets denominator to 1 Rational(); // sets numerator to o, denominator to 1 friend Rational operator+(// complete the arguments); friend Rational operator-1//complete the arguments); friend Rational operator (1/complete the arguments); friend Rational operator/// complete the arguments); friend bool operators // complete the arguments); friend bool operatorc='//complete the arguments); friend bool operator>(// complete the arguments); friend bool operator // complete the arguments); friend bool operator == // complete the arguments); friend ostream& operator >(1/ complete the arguments); private: int n; int d; }; void normalize (int En, int d); 1/Implementations of the members of class Rational 1/private members of class Rational int n; int d; Rational:: Rational (int numer, int denom) { normalize (numer, denom); n = numer; d = denom; 1 1/sets denominator to 1 Rational:: Rational (int numer) { // Complete the function 1 // sets numerator to 0, denominator to 1 Rational:: Rational { //Complete the function 1 Rational operator +(/complete the arguments) 1 1/Complete the function ) Rational operator - 11/complete the arguments) //Complete the function } Rational operator = (/complete the arguments) 1 //Complete the function } Rational operator/1/complete the arguments) //Complete the function } 1/precondition: all relational operators require d > 0 bool operator //complete the arguments) { //Complete the function } bool operator >= 1/complete the arguments) { 1/Complete the function 1 bool operator==(1/complete the arguments) 1/Complete the function } { / /NOTE: //The first param MUST NOT be const. The // second one is written, so it cannot be const either. istream& operator>>(istream: in_sty, Rationalt right) //Complete the function normalize (right.n, right.d); return in_ate; } { //The first parameter should not be const, the // second is read only and should be const. ostream& operator = n so algorithm will work! { } int ri while(r = 0) } return m; } 1/postcondition: n and d to be numerator and denominator 1/of a fraction) have all common factors removed, and a > 0. void normalize (inte n, ints d) { // remove common factors: //Complete the function 1/fix things so that if the fraction is 'negative //it is n that carries the sign. It both n and d are /egative, each is made positive. //Complete the function 1 // Testing part is included in the main function. You do not need to write them int main() { cout > overloading: Enter" > cout " :) Kendi cou: " ) 1) =m) 1) w) = " = w)

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!