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 #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 #include 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
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 now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images