Question: Using C++, write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator
Using C++, write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator and the denominator. For example, the fraction 5/6 would have a denominator of 5 and a numerator of 6. Include a constructor with two arguments that can be used to set the numerator and denominator (forbidding zero in the denominator). Provide default values of zero for the numerator and one for the denominator. Overload the input and output operators. Numbers are to be read and written in the form 1/2, 32/15, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2, 32/-15, and -300/-401 are possible. Include a function to normalize the values stored so that, after normalization, the denominator is positive and as small as possible. For example, after normalization, 4/-8 would be represented the same as -1/2. Overload the usual arithmetic operators to provide addition, subtraction, multiplication, and division of two rational numbers. Overload the usual comparison operations to allow comparison of two rational numbers. Create rational.cpp using the header file rational.h and the test file test_rational.cpp.
rational.h
#ifndef _RATIONAL_H_ #define _RATIONAL_H_ #includeclass rational { // class invariant: rational number has a positive denominator & is in // lowest terms public: rational (int n, int d = 1); // precondition: d is not 0 // postcondition: rational number with specified attributes has been created int get_num() const; // returned: numerator of rational number int get_denom() const; // returned: denominator of rational number private: void reduce(); // postcondition: rational number satisfies class invariant int num; int denom; }; rational operator + (const rational& r1, const rational& r2); // returned: sum of r1 & r2 rational operator - (const rational& r1, const rational& r2); // returned: difference of r1 & r2 rational operator * (const rational& r1, const rational& r2); // returned: product of r1 & r2 rational operator / (const rational& r1, const rational& r2); //precondition: r2 is not 0 // returned: quotient of r1 & r2 bool operator == (const rational& r1, const rational& r2); // returned: whether r1 & r2 are equal bool operator != (const rational& r1, const rational& r2); // returned: whether r1 & r2 are not equal bool operator < (const rational& r1, const rational& r2); // returned: whether r1 < r2 bool operator <= (const rational& r1, const rational& r2); // returned: whether r1 <= r2 bool operator > (const rational& r1, const rational& r2); // returned: whether r1 > r2 bool operator >= (const rational& r1, const rational& r2); // returned: whether r1 >= r2 std::ostream& operator << (std::ostream& out, const rational& r); // postcondition: r has been sent to out in format n/r #endif
test_rational.cpp
#include#include #include #include "rational.h" using namespace std; int main () { rational r1(-3, -6); assert (r1.get_num() == 1); assert (r1.get_denom() == 2); rational r2 (0, -5); assert (r2.get_num() == 0); assert (r2.get_denom() == 1); rational r3 (8, -4); assert (r3.get_num() == -2); assert (r3.get_denom() == 1); rational r4 (-3, 8); assert (r4.get_num() == -3); assert (r4.get_denom() == 8); rational r5 = r1 + r4; assert (r5.get_num() == 1); assert (r5.get_denom() == 8); rational r6 = r1 - r4; assert (r6.get_num() == 7); assert (r6.get_denom() == 8); rational r7 = r1 * r4; assert (r7.get_num() == -3); assert (r7.get_denom() ==16); rational r8 = r1 / r4; assert (r8.get_num() == -4); assert (r8.get_denom() == 3); rational r9 (8, 16); assert (r1 == r9); assert (r4 != r9); assert (r8 < r1); assert (r9 <= r1); assert (r5 > r7); assert (r5 >= r7); cout << r6 << endl; cout << "all tests passed - do endzone celebration dance!" << endl; return EXIT_SUCCESS; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
