Use C++ Modify the code to have operator overloading functions with the following operators. ==,  <,  <=,  >,  >=,  +,  -,  *,  / Use the Rational interface and main code in the next page. I need three files one main.cpp, one rational.cpp file and one rational.h file.  Main file and h file are shown on the picture they don't need to be changed I just need rational.cpp file. Here is the rational.cpp file I have: #include "rational.h" Rational::Rational() { numerator = 0; denominator = 1; } Rational::Rational(int num, int den) { numerator = num; denominator = den; } istream& operator >>(istream& take, Rational& r1) { string userInput = " "; string num = " "; string den = " "; bool findSlash = false; take >> userInput; for (int i = 0; i < userInput.length(); i++) { if (userInput[i] != '/' && !findSlash) { num = num + userInput[i]; } else if (userInput[i] == '/') { findSlash = true; } else if (userInput[i] != '/' && !findSlash) { den = den + userInput[i]; } } r1.numerator = stoi(num); r1.denominator = stoi(den); return take; } ostream& operator<<(ostream& out, const Rational& r1) { Rational temp(r1.numerator, r1.denominator); temp.simplify(); out << temp.numerator << "/" << temp.denominator; return out; } Rational operator+(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.denominator + r.numerator * f.denominator), (f.denominator * r.denominator)); return temp; } Rational operator-(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.denominator - f.denominator * r.numerator), (f.denominator * r.denominator)); return temp; } Rational operator*(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.numerator), (f.denominator * r.denominator)); return temp; } Rational operator/(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.denominator), (f.denominator * r.numerator)); return temp; } bool operator==(const Rational& f, const Rational& r) { } bool operator>(const Rational&, const Rational&) { } bool operator<(const Rational&, const Rational&) { } bool operator>=(const Rational&, const Rational&) { } bool operator<=(const Rational&, const Rational&) { } void Rational::simplify() { }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Use C++

Modify the code to have operator overloading functions with the following operators.
==,  <,  <=,  >,  >=,  +,  -,  *,  /
Use the Rational interface and main code in the next page.

I need three files one main.cpp, one rational.cpp file and one rational.h file.  Main file and h file are shown on the picture they don't need to be changed I just need rational.cpp file.

Here is the rational.cpp file I have:

#include "rational.h"
Rational::Rational()
{
numerator = 0;
denominator = 1;
}

Rational::Rational(int num, int den)
{
numerator = num;
denominator = den;
}

istream& operator >>(istream& take, Rational& r1)
{
string userInput = " ";
string num = " ";
string den = " ";
bool findSlash = false;
take >> userInput;
for (int i = 0; i < userInput.length(); i++)
{
if (userInput[i] != '/' && !findSlash)
{
num = num + userInput[i];
}
else if (userInput[i] == '/')
{
findSlash = true;
}
else if (userInput[i] != '/' && !findSlash)
{
den = den + userInput[i];
}
}
r1.numerator = stoi(num);
r1.denominator = stoi(den);
return take;

}

ostream& operator<<(ostream& out, const Rational& r1)
{
Rational temp(r1.numerator, r1.denominator);
temp.simplify();
out << temp.numerator << "/" << temp.denominator;
return out;

}

Rational operator+(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.denominator + r.numerator * f.denominator), (f.denominator * r.denominator));
return temp;
}

Rational operator-(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.denominator - f.denominator * r.numerator), (f.denominator * r.denominator));
return temp;
}

Rational operator*(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.numerator), (f.denominator * r.denominator));
return temp;
}

Rational operator/(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.denominator), (f.denominator * r.numerator));
return temp;
}

bool operator==(const Rational& f, const Rational& r)
{

}

bool operator>(const Rational&, const Rational&)
{

}

bool operator<(const Rational&, const Rational&)
{

}

bool operator>=(const Rational&, const Rational&)
{

}

bool operator<=(const Rational&, const Rational&)
{

}

void Rational::simplify()
{

}

 

 

```cpp
#include<iostream>
#include<cstdlib>
using namespace std;

class Rational
{
public:
    Rational();
    Rational(int);
    Rational(int, int);

    friend istream& operator >>(istream&, Rational&); // input function
    friend ostream& operator <<(ostream&, const Rational&); // output function

    // Arithmetic operators (+, -, *, /)
    friend Rational operator+(const Rational&, const Rational&);
    friend Rational operator-(const Rational&, const Rational&);
    friend Rational operator*(const Rational&, const Rational&);
    friend Rational operator/(const Rational&, const Rational&);

    // Relational operators (==, >, <, >=, <=)
    friend bool operator==(const Rational&, const Rational&);
    friend bool operator>(const Rational&, const Rational&);
    friend bool operator<(const Rational&, const Rational&);
    friend bool operator>=(const Rational&, const Rational&);
    friend bool operator<=(const Rational&, const Rational&);

private:
    int numerator;
    int denominator;
    void simplify(); // make 2/4 --> 1/2
};
```

**Explanation:**

This C++ header file, `rational.h`, defines a class called `Rational` for representing rational numbers (fractions). Here's a breakdown of its components:

1. **Includes and Namespace:**
   - Includes the iostream and cstdlib libraries.
   - Uses the standard namespace.

2. **Class Definition:**
   - The `Rational` class includes public constructors for creating rational numbers with no arguments, or with one or two integer arguments.
   
3. **Friend Functions:**
   - For input and output operations: `operator>>` and `operator<<`.
   - For arithmetic operations: `operator+`, `operator-`, `operator*`, `operator/`.
   - For relational comparisons: `operator==`, `operator>`, `operator<`, `operator>=`, `operator<=`.

4. **Private Members:**
   - Contains two integers: `numerator` and `denominator`.
   - A `simplify` function to reduce fractions to their simplest form.

This header file would be included in a project that requires arithmetic and relational operations on rational numbers, ensuring they can be used as easily as built-in types.
Transcribed Image Text:```cpp #include<iostream> #include<cstdlib> using namespace std; class Rational { public: Rational(); Rational(int); Rational(int, int); friend istream& operator >>(istream&, Rational&); // input function friend ostream& operator <<(ostream&, const Rational&); // output function // Arithmetic operators (+, -, *, /) friend Rational operator+(const Rational&, const Rational&); friend Rational operator-(const Rational&, const Rational&); friend Rational operator*(const Rational&, const Rational&); friend Rational operator/(const Rational&, const Rational&); // Relational operators (==, >, <, >=, <=) friend bool operator==(const Rational&, const Rational&); friend bool operator>(const Rational&, const Rational&); friend bool operator<(const Rational&, const Rational&); friend bool operator>=(const Rational&, const Rational&); friend bool operator<=(const Rational&, const Rational&); private: int numerator; int denominator; void simplify(); // make 2/4 --> 1/2 }; ``` **Explanation:** This C++ header file, `rational.h`, defines a class called `Rational` for representing rational numbers (fractions). Here's a breakdown of its components: 1. **Includes and Namespace:** - Includes the iostream and cstdlib libraries. - Uses the standard namespace. 2. **Class Definition:** - The `Rational` class includes public constructors for creating rational numbers with no arguments, or with one or two integer arguments. 3. **Friend Functions:** - For input and output operations: `operator>>` and `operator<<`. - For arithmetic operations: `operator+`, `operator-`, `operator*`, `operator/`. - For relational comparisons: `operator==`, `operator>`, `operator<`, `operator>=`, `operator<=`. 4. **Private Members:** - Contains two integers: `numerator` and `denominator`. - A `simplify` function to reduce fractions to their simplest form. This header file would be included in a project that requires arithmetic and relational operations on rational numbers, ensuring they can be used as easily as built-in types.
### Code Explanation

The provided C++ code snippet performs arithmetic operations on rational numbers. Here's the breakdown:

```cpp
int main()
{
    Rational r1, r2;
    char answer = 'y';

    while (answer == 'y') {
        cout << "Enter the first fraction (e.g. 3/4) : ";
        cin >> r1;
        cout << "Enter the second fraction (e.g. 3/4) : ";
        cin >> r2;
        cout << "r1 = " << r1 << endl;
        cout << "r2 = " << r2 << endl;
        cout << "r1 + r2 = " << r1 + r2 << endl;
        cout << "r1 - r2 = " << r1 - r2 << endl;
        cout << "r1 * r2 = " << r1 * r2 << endl;
        cout << "r1 / r2 = " << r1 / r2 << endl;
        cout << "r1 == r2 -> " << (r1 == r2) << endl;
        cout << "r1 != r2 -> " << (r1 != r2) << endl;
        cout << "r1 <= r2 -> " << (r1 <= r2) << endl;
        cout << "r1 >= r2 -> " << (r1 >= r2) << endl;
        cout << "Again (y/n)? ";
        cin >> answer;
    }

    return 0;
}
```

**Description:**

- **Rational r1, r2;**: Instantiates two Rational objects, `r1` and `r2`.
- **char answer = 'y';**: Initializes a character variable `answer` to control the loop.
- **while (answer == 'y')**: A loop that continues as long as the user wants to perform more operations.
- **cin and cout**: Used for input and output, specifically for entering fractions and displaying results.
- **Arithmetic and Comparison**: Performs addition, subtraction, multiplication, division, and comparison (`==`, `!=`, `<=`, `>=`) on two rational numbers.

### Output Example

The output display shows step-by-step calculations with user inputs and results:

- **Input:** Enter the first fraction (e.g. 3/4): 3/3
- **
Transcribed Image Text:### Code Explanation The provided C++ code snippet performs arithmetic operations on rational numbers. Here's the breakdown: ```cpp int main() { Rational r1, r2; char answer = 'y'; while (answer == 'y') { cout << "Enter the first fraction (e.g. 3/4) : "; cin >> r1; cout << "Enter the second fraction (e.g. 3/4) : "; cin >> r2; cout << "r1 = " << r1 << endl; cout << "r2 = " << r2 << endl; cout << "r1 + r2 = " << r1 + r2 << endl; cout << "r1 - r2 = " << r1 - r2 << endl; cout << "r1 * r2 = " << r1 * r2 << endl; cout << "r1 / r2 = " << r1 / r2 << endl; cout << "r1 == r2 -> " << (r1 == r2) << endl; cout << "r1 != r2 -> " << (r1 != r2) << endl; cout << "r1 <= r2 -> " << (r1 <= r2) << endl; cout << "r1 >= r2 -> " << (r1 >= r2) << endl; cout << "Again (y/n)? "; cin >> answer; } return 0; } ``` **Description:** - **Rational r1, r2;**: Instantiates two Rational objects, `r1` and `r2`. - **char answer = 'y';**: Initializes a character variable `answer` to control the loop. - **while (answer == 'y')**: A loop that continues as long as the user wants to perform more operations. - **cin and cout**: Used for input and output, specifically for entering fractions and displaying results. - **Arithmetic and Comparison**: Performs addition, subtraction, multiplication, division, and comparison (`==`, `!=`, `<=`, `>=`) on two rational numbers. ### Output Example The output display shows step-by-step calculations with user inputs and results: - **Input:** Enter the first fraction (e.g. 3/4): 3/3 - **
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education