Question: In C++, please help create a new file to work for an operator overload and rational number objects. I want some explanation with comments in

In C++, please help create a new file to work for an operator overload and rational number objects. I want some explanation with comments in the new file which I want to call Rational.cpp. I was given two files that will not be edited as the point was to create a new file and work the other two files in. Also, maybe add in another h file but I don't know. In the new file called rational.cpp I need to create a class called Rational that holds two values of the numerator and denominator of a rational fraction. Also, provide the following operators as member functions: *, /, +, -, =, ==, and !=.

Overload <, and conversion to double and provide two constructors as well. Finally, add safety with no common factor for the numerator and denominator other than 1, in which the denominator of 0 is 1. The denominator is positive, and the negative numbers have a negative numerator.

The two files being provided must not be changed; all code can work with the new file.

Assert.h

#ifndef ASSERT_H #define ASSERT_H #include #include using std::cout; using std::endl; using std::string; class Assert { public: static void isTrue(bool actual) { if (actual) cout << "OK" << endl; else cout << "FAIL" << endl; } static void isFalse(bool actual) { isTrue(!actual); } static void equals(int expected, int actual) { isTrue (expected == actual); } static void equals(string expected, string actual) { isTrue (expected.compare(actual) == 0); } template static void equals(const T& expected, const T& actual) { isTrue(expected == actual); } }; #endif

hw3.cpp

#include #include #include "Assert.h" #include "Rational.h" using namespace std; int main() { Rational a(1, 3); Rational b(100, 1); Rational x(0, 0); cout << "Handles zero over anything as 0 over 1 "; Assert::equals(x, a * x); Rational y(99, 33); cout << "Normalizes Rational(99,33) in the constructor. "; Assert::equals(Rational(3,1), y); cout << a << " * " << b << " = " << a * b << endl; Assert::equals(Rational(100, 3), a * b); a = Rational(3, 7); b = Rational(1, 7); cout << a << " / " << b << " = " << a / b << endl; Assert::equals(Rational(3, 1), a / b); a = Rational(1, 3); b = Rational(2, 5); Assert::isTrue(a != b); Assert::isFalse(a == b); Rational c = a + b; cout << a << " + " << b << " = " << c << endl; Assert::equals(Rational(11, 15), c); a = Rational(2, 3); b = Rational(1, 6); cout << a << " - " << b << " = " << a - b << endl; Assert::equals(Rational(1, 2), a - b); Assert::equals(Rational(0, 1), a - a); a = b = c; cout << a << " = " << b << " = " << c << endl; Assert::equals(Rational(11, 15), a); Assert::isTrue(b == c); double dValue = a; cout << fixed << setprecision(11) << dValue << endl; return 0; }

The output that we should get:

Handles zero over anything as 0 over 1 OK Normalizes Rational(99,33) in the constructor. OK (1/3) * (100/1) = (100/3) OK (3/7) / (1/7) = (3/1) OK OK OK (1/3) + (2/5) = (11/15) OK (2/3) - (1/6) = (1/2) OK OK (11/15) = (11/15) = (11/15) OK OK 0.73333333333

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!