
Use C++
I need three files, one main.cpp file, one rational.cpp file and one rational.h file. I'll put the code for the main.cpp file and rational.h file below. No need to change them I just need the rational.cpp file.
Modify the code to have operator overloading functions with the following operators.
==, <, <=, >, >=, +, -, *, /
Use the Rational interface and main code in the next page.
main.cpp:
#include <iostream>
#include "rational.h"
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 << 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 << 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 << endl;
cout << "Again (y/n)? ";
cin >> answer;
}
return 0;
}
rational.h:
#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 the code run the same as in the picture


Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- C++ Write a function named “createPurchaseOrder” that accepts the quantity(integer), the cost per item (double), and the description (string). It will return anewly created PurchaseOrder object holding that information if they are valid:quantity and cost per item cannot be negative and the description cannot be empty or blank(s). When it is invalid, it will return NULL to indicate that it cannot create such PurchaseOrder object. PurchaseOrder createPurchaseOrder(int quantity, double costPerItem, string description) { if(quantity < 0 || costPerItem < 0.0 || description == "") return NULL; else { PurchaseOrder p = new PurchaseOrder(quantity,costPerItem,description); return p; } } what's the issue with this function and how to rewrite itarrow_forwardIn C++ compile a program to fufill purpose mentioned below, not copied from internet please. Write a program that uses a structure named MovieData to store the following information about a movie:TitleDirectorYear ReleasedRunning Time (in minutes)The program should create two MovieData variables, store values in their members, and pass each one, in turn, to a function that displays the information about the movie in a clearly formatted manner.arrow_forwardC program: Please write a program with call to functions to produce the output given below: *********************************************** * MENU – Final Exam * * (1) Calling displayOddDigitInfo() * * (2) Quit * *********************************************** Enter an integer for option + ENTER: 6 Wrong Option! *********************************************** * MENU – Final Exam * * (1) Calling displayOddDigitInfo() * * (2) Quit * *********************************************** Enter an integer for option + ENTER: 1 Enter an integer: -294257 Calling displayOddDigitInfo() with argument of -294257 - While displayOddDigitInfo() is running – -294257 is negative and odd! -294257 has 3 odd digits of 7 5 9 *********************************************** * MENU – Final Exam * * (1) Calling displayOddDigitInfo() * * (2) Quit * *********************************************** Enter an integer for option + ENTER: 1 Enter an integer: -25904 Calling displayOddDigitInfo() with argument of…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





