Question

In: Computer Science

C++ Program Overload the following operators to work with the Rational class and add test cases...

C++ Program

Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class.

+




*


/


==


!=


<


<=


>


>=


<< (stream insertion operator, use the toString function)


>> (stream extraction operator)


Make sure you test all 12 operators


Example Run (Bold is input), everything else is a print statement using the overloaded operators


Enter a rational number a/b >> c : 1/3


Enter another rational number c/d >> d: 2/4


c << 1/3


d << 1/2


1/3 + 1/2 = 5/6


1/3 - 1/2 = -1/6


1/3 * 1/2 = 1/6


1/3 / 1/2 = 2/3


1/3 > 1/2 is false


1/3 < 1/2 is true


1/3 >= 1/2 is false


1/3 <= 1/2 is true


1/3 == 1/2 is false


1/3 != 1/2 is true


Solutions

Expert Solution

/*** Rational.h ***/

#ifndef RATIONAL_H
#define RATIONAL_H

#include<iostream>

using namespace std;

class Rational
{
public:
Rational(int num=0, int denom=1);
//mutators / setters
void set(int num, int denom);
void set_num(int n);
void set_denom(int d);

//accessors / getters
int get_num() const;
int get_denom() const;

//will return object whose error state is set
// if one of the operands is in error.
friend Rational operator +(const Rational &num1, const Rational &num2);
friend Rational operator -(const Rational &num1, const Rational &num2);
friend Rational operator *(const Rational &num1, const Rational &num2);
friend Rational operator /(const Rational &num1, const Rational &num2);

friend bool operator ==(const Rational &num1, const Rational &num2);
friend bool operator !=(const Rational &num1, const Rational &num2);
friend bool operator <(const Rational &num1, const Rational &num2);
friend bool operator >(const Rational &num1, const Rational &num2);
friend bool operator <=(const Rational &num1, const Rational &num2);
friend bool operator >=(const Rational &num1, const Rational &num2);
//will print UNDEFINED if num is in error.
friend ostream& operator <<(ostream &outs, const Rational &num);
friend istream& operator >>(istream &ins, Rational &num);

void simplify();

private:
int _n; //numerator
int _d; //denominator
};

#endif // RATIONAL_H

/*** Rational.cpp ***/

#include <iostream>
#include "Rational.h"

using namespace std;

//sets digits numerator and denominator
Rational::Rational(int num, int denom) {
_n = num;
_d = denom;
}

void Rational::set(int num, int denom) {
_n = num;
_d = denom;
}

void Rational::set_num(int n) {
_n = n;
}

void Rational::set_denom(int d) {
_d = d;
}

int Rational::get_num() const {
return _n;
}

int Rational::get_denom() const {
return _d;
}

// // //////////////////////operators//////////////////////////////
Rational operator +(const Rational &num1, const Rational &num2) {
int n = num1._n*num2._d + num1._d*num2._n;
int d = num1._d*num2._d;
Rational r(n,d);
r.simplify();
return r;
}

Rational operator -(const Rational &num1, const Rational &num2) {
int help = num2._n *-1;
int n = num1._n*num2._d + num1._d*help;
int d = num1._d*num2._d;
Rational r(n,d);
r.simplify();
return r;
}

Rational operator *(const Rational &num1, const Rational &num2) {
int n = num1._n*num2._n;
int d = num1._d*num2._d;
Rational r(n,d);
r.simplify();
return r;
}

Rational operator /(const Rational &num1, const Rational &num2) {
int n = num1._n*num2._d;
int d = num1._d*num2._n;
Rational r(n,d);
r.simplify();
return r;
}

ostream& operator <<(ostream &outs, const Rational &num){
if(num.get_denom()>0)
outs<<num._n<<"/"<<num._d;
else if(num.get_denom()<0)
outs<<-num._n<<"/"<<-num._d;
else
outs<<"UNDEFINED";
return outs;
}

istream& operator >>(istream &ins, Rational &num){
int n,d;
char slash;
ins>>n>>slash>>d;
num = Rational(n,d);
return ins;
}

// COMPARSON OPERATORS
bool operator ==(const Rational &num1, const Rational &num2){
double compare = double(num1._n)/num1._d - double(num2._n)/num2._d;
if(compare == 0) return true;
return false;
}

bool operator !=(const Rational &num1, const Rational &num2){
double compare = double(num1._n)/num1._d - double(num2._n)/num2._d;
if(compare != 0) return true;
return false;
}

bool operator <(const Rational &num1, const Rational &num2){
double compare = double(num1._n)/num1._d - double(num2._n)/num2._d;
if(compare < 0) return true;
return false;
}

bool operator <=(const Rational &num1, const Rational &num2){
double compare = double(num1._n)/num1._d - double(num2._n)/num2._d;
if(compare <= 0) return true;
return false;
}


bool operator >(const Rational &num1, const Rational &num2) {
double compare = double(num1._n)/num1._d - double(num2._n)/num2._d;
if(compare > 0) return true;
return false;
}

bool operator >=(const Rational &num1, const Rational &num2) {
double compare = double(num1._n)/num1._d - double(num2._n)/num2._d;
if(compare >= 0) return true;
return false;
}

int gcd(int a, int b)
{
if(a==0)
return b;
return gcd(b%a, a);
}

void Rational::simplify()
{
int g = gcd(_n, _d);
_n /= g;
_d /= g;
}

/*** main.cpp ***/

#include <iostream>
#include "rational.h"

using namespace std;

//main function
int main()
{
Rational a, c, d;

cout<<"Enter Rational number a/b: ";
cin>>c;
cout<<"Enter Rational number c/d: ";
cin>>d;

cout<<c<<endl;
cout<<d<<endl;

a = c + d;
cout<<c<<" + "<<d<< " = "<<a<<endl;

a = c - d;
cout<<c<<" - "<<d<< " = "<<a<<endl;

a = c * d;
cout<<c<<" * "<<d<< " = "<<a<<endl;

a = c / d;
cout<<c<<" / "<<d<< " = "<<a<<endl;

cout<<boolalpha;
bool f = c > d;
cout<<c<<" > "<<d<< " is "<<f<<endl;

f = c < d;
cout<<c<<" < "<<d<< " is "<<f<<endl;

f = c >= d;
cout<<c<<" >= "<<d<< " is "<<f<<endl;

f = c <= d;
cout<<c<<" <= "<<d<< " is "<<f<<endl;

f = c == d;
cout<<c<<" == "<<d<< " is "<<f<<endl;

f = c != d;
cout<<c<<" != "<<d<< " is "<<f<<endl;

return 0;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >=...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >= as member operators. The comparison operators will compare students by last name first name and id number. Also, overload << and >> as friend operators in the Student class. Overload << and >> operators in Roster class as well to output Rosters in a nice format as well as input Roster. Provide a function sort in a Roster class as a private function to...
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==,...
This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c. 1.Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must...
Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload...
Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload and test function doubleNum(int) to also work with a parameter of type double. 3. Both functions for collecting input should validate such that negative values would not be allowed. Perform the following steps: a. Add the function prototypes at the top. b. In main(), add a new variable of type double. You can name it "value2" to distinguish from the other variable. c. Write...
Take the following program and include overload functions into it. Display result of function. C++ Program:...
Take the following program and include overload functions into it. Display result of function. C++ Program: #include <iostream> using namespace std; // this is the additional function string read() {     string input;     cout << "Enter input: ";     cin >> input;     return input; } int main() {     // call function here     string output = read();     cout << "You entered: " << output; }
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
Write a rational number class in C++. Recall a rational number is a rational number, composed...
Write a rational number class in C++. Recall 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. You should represent rational numbers using 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 the following two constructors: one constructor to make...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the...
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the widths for the 2 Rectangles and multiple the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the non-member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE. Using the program below, overload the Divide...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT