C++ I am using visual studio for it.
Make a copy of the Rational class you created in the previous
Lab. Modify the class. Replace all your mathematical, input, and
output functions with overloaded operators.
Overload the following 12 operators:
+ - * / < > = = ! = <= >= >> <<
In order to test your class in your main function, prompt the user
for a numerator and a denominator for your first object. Repeat the
prompt for the second object. Perform all mathematical operations
and comparisons using your overloaded operators. Print the two
Rational numbers using your new overloaded operator “<<”.
Then print the result of all the calculations using your overloaded
operators. Make sure you label your output, for instance:
Your first rational number is: 3/2. Your second number is 5/2. 5/2
+ 3/2 = 4/1 (using “+” operator)
…………………………………………
5/2 > 3/2 is true. (using “>” operator) 5/2 < 3/2 is
false. (using “<” operator)
…………………………………………
Output the rest of the results the same way. You should have 12
lines displaying the results; 2 to output the numbers and 10 to
display the results of using the overloaded operators.
Note that in the above example 3/2 + 5/2 = 8/2. Your class stores
the fraction in the reduced form, 4/1.
All the work must be done inside the class. The client (main
function) should only use the member or the friend functions of the
objects. Do not perform any calculation or comparison in the main
function.
Create two friend functions for overloading insertion and
extraction operators. The rest of the functions must be member
functions. Use the following example to create the member
functions.
Function definition: passing a Rational object to the member
function of another.
Rational Rational :: operator* (const Rational &num)
const
{
Rational temp;
temp.numerator = numerator*num.numerator;
temp.denominator = denominator*num.denominator; //call the helper
function to reduce the fraction temp.simplify( ); return
temp;
}
In your main function:
Rational num1, num2, result; //get values from the user for num1
cin>>num1; //more codes here
result = num1 + num2 ;
//label all your outputs properly cout<<result;
As usual, add all necessary comments. Run your program with several
test data. Capture the screen image and save it. Test, and test,
and test! Make sure you test all possible cases, including a zero
as the denominator.
/////////////////////////////////////////////////////
//Here is the coding i used for the last lab.How would i use overloading operators for this assignment?
// Rational.h
#ifndef RATIONAL_H
#define RATIONAL_H
#include<iostream>
class Rational
{
private:
//Arguments have to be positive
int numerator;
int denominator;
void simplify();
public:
//Constructors
Rational();
Rational(int numerator, int denominator);
Rational add(Rational num);
Rational subtract(Rational num);
Rational multiply(Rational num);
Rational divide(Rational num);
//return fraction
int getNumerator() const;
int getDenominator() const;
void setNumerator(int numer);
void setDenominator(int denom);
void printRational();
void printFloating();
};1;
#endif
_____________________________
// Rational.cpp
#include "Rational.h"
#include<iostream>
using namespace std;
//Constuctors
Rational::Rational()
{
this->numerator=0;
this->denominator=1;
}
Rational Rational::add(Rational num)
{
Rational temp;
temp.numerator = numerator * num.denominator + denominator *
num.numerator;
temp.denominator= denominator * num.denominator;
temp.simplify();
return temp;
}
Rational Rational::subtract(Rational num)
{
Rational temp;
temp.numerator = numerator * num.denominator - denominator *
num.numerator;
temp.denominator = denominator * num.denominator;
temp.simplify();
return temp;
}
Rational Rational::multiply(Rational num)
{
Rational temp;
temp.numerator = numerator * num.numerator;
temp.denominator = denominator * num.denominator;
temp.simplify();
return temp;
}
Rational Rational::divide(Rational num)
{
Rational temp;
temp.numerator = denominator * num.numerator;
temp.denominator = numerator * num.denominator;
temp.simplify();
return temp;
}
void Rational::printRational()
{
if (numerator < 0 && denominator < 0) {
setNumerator(numerator * -1);
setDenominator(denominator * -1);
} else if (denominator < 0) {
setNumerator(numerator * -1);
setDenominator(denominator * -1);
}
if (denominator == 0)
cout << "\nDivid by zero error!!!\n";
else if (numerator == 0)
cout << 0;
else
cout << numerator << "/" << denominator;
}
void Rational::printFloating()
{
cout << static_cast < double > (numerator) /
denominator;
}
void Rational::simplify()
{
int largest;
int gcd = 0;
largest = numerator > denominator ? numerator :
denominator;
for (int i = 2; i <= largest; i++)
if (numerator % i == 0 && denominator % i == 0)
gcd = i;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
}
}
int Rational::getNumerator() const
{
return numerator;
}
int Rational::getDenominator() const
{
return denominator;
}
void Rational::setNumerator(int numer)
{
this->numerator=numer;
}
void Rational::setDenominator(int denom)
{
this->denominator=denom;
}
Rational::Rational(int numerator, int denominator)
{
setNumerator(numerator);
setDenominator(denominator);
}
_______________________________
// main.cpp
#include <iostream>
#include"Rational.h"
using namespace std;
int main()
{
Rational num1, num2, result;
//get values from the user for num1 and num2
int n1, d1, n2, d2;
cout << ":: Rational Number#1 ::" << endl;
cout << "Enter Numerator :";
cin >> n1;
num1.setNumerator(n1);
cout << "Enter Denominator :";
cin >> d1;
num1.setDenominator(d1);
cout << ":: Rational Number#2 ::" << endl;
cout << "Enter Numerator :";
cin >> n2;
num2.setNumerator(n2);
cout << "Enter Denominator :";
cin >> d2;
num2.setDenominator(d2);
// Creating the objects to RationalNumber class
int choice;
//Use a loop to allow the user to continue using this
program
while (choice!=5)
{
cout << "\n\n1. Add" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Mulitply" << endl;
cout << "4. Divide" << endl;
cout << "5. Exit" << endl;
cout << "Enter a choice: ";
cin >> choice;
if (choice == 1)
{
result = num1.add(num2);
num1.printRational();
cout<<" + ";
num2.printRational();
cout<<" = ";
result.printRational();
cout<<endl;
cout<<"Result In Decimal :";
result.printFloating();
cout<<endl;
}
else if(choice==2)
{
result = num1.subtract(num2);
num1.printRational();
cout<<" - ";
num2.printRational();
cout<<" = ";
result.printRational();
cout<<endl;
cout<<"Result In Decimal :";
result.printFloating();
}
else if(choice==3)
{
result = num1.multiply(num2);
num1.printRational();
cout<<" * ";
num2.printRational();
cout<<" = ";
result.printRational();
cout<<endl;
cout<<"Result In Decimal :";
result.printFloating();
}
else if(choice==4)
{
result = num1.divide(num2);
num1.printRational();
cout<<" / ";
num2.printRational();
cout<<" = ";
result.printRational();
cout<<endl;
cout<<"Result In Decimal :";
result.printFloating();
}
else if(choice==5)
{
break;
}
else{
cout<<"** Invalid Choice **"<<endl;
}
}
return 0;
}
// Rational.h
#ifndef RATIONAL_H
#define RATIONAL_H
#include<iostream>
class Rational
{
private:
int numerator;
int denominator;
void simplify();
public:
Rational();
Rational(int num, int den);
void setNumerator(int num);
void setDenominator(int den);
int getNumerator();
int getDenominator();
Rational operator+(const Rational& secondRational)
const;
Rational operator-(const Rational& secondRational)
const;
Rational operator*(const Rational& secondRational)
const;
Rational operator/(const Rational& secondRational)
const;
bool operator<(const Rational& r2) const;
bool operator<=(const Rational& r2)
const;
bool operator>(const Rational& r2) const;
bool operator>=(const Rational& r2)
const;
bool operator==(const Rational& r2) const;
bool operator!=(const Rational& r2) const;
friend ostream& operator<<(ostream& ,
const Rational&);
friend istream& operator>>(istream& ,
Rational&);
void printRational();
void printFloating();
};
#endif
//end of Rational.h
// Rational.cpp
#include "Rational.h"
#include <iostream>
using namespace std;
// default constructor
Rational::Rational()
{
numerator = 0;
denominator = 1;
}
// parameterized constructor
Rational::Rational(int num, int den)
{
setNumerator(num);
setDenominator(den);
simplify();
}
// function to simplify the rational number to its simplest
form
void Rational::simplify()
{
int largest;
int gcd = 0;
largest = numerator > denominator ? numerator :
denominator;
for (int i = 2; i <= largest; i++)
if (numerator % i == 0 && denominator % i == 0)
gcd = i;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
}
}
// function to set the numerator
void Rational:: setNumerator(int num)
{
numerator = num;
}
// function to set the denominator
void Rational:: setDenominator(int den)
{
denominator = den;
}
// function to return the numerator
int Rational:: getNumerator()
{
return numerator;
}
// function to return the denominator
int Rational:: getDenominator()
{
return denominator;
}
// Define function operators for augmented operators
// function to return the Rational number obtained by adding 2
Rational numbers
Rational Rational::operator+(const Rational& secondRational)
const
{
Rational temp;
temp.numerator = numerator *
secondRational.denominator + denominator *
secondRational.numerator;
temp.denominator = denominator *
secondRational.denominator;
temp.simplify();
return temp;
}
// function to return the Rational number obtained by
subtracting 2 Rational numbers
Rational Rational::operator-(const Rational& secondRational)
const
{
Rational temp;
temp.numerator = numerator *
secondRational.denominator- denominator *
secondRational.numerator;
temp.denominator = denominator *
secondRational.denominator;
temp.simplify();
return temp;
}
// function to return the Rational number obtained by
multiplying 2 Rational numbers
Rational Rational::operator*(const Rational& secondRational)
const
{
Rational temp;
temp.numerator =
numerator*secondRational.numerator;
temp.denominator =
denominator*secondRational.denominator;
temp.simplify();
return temp;
}
// function to return the Rational number obtained by dividing 2
Rational numbers
Rational Rational::operator/(const Rational& secondRational)
const
{
Rational temp;
temp.numerator = numerator *
secondRational.denominator;
temp.denominator = denominator *
secondRational.numerator;
temp.simplify();
return temp;
}
// function that returns true if this rational number < r2,
else false
bool Rational:: operator<(const Rational& r2) const
{
double r1Val = ((double)numerator)/denominator;
double r2Val =
((double)r2.numerator)/r2.denominator;
return (r1Val < r2Val);
}
// function that returns true if this rational number <= r2,
else false
bool Rational:: operator<=(const Rational& r2) const
{
double r1Val = ((double)numerator)/denominator;
double r2Val =
((double)r2.numerator)/r2.denominator;
return (r1Val <= r2Val);
}
// function that returns true if this rational number > r2,
else false
bool Rational:: operator>(const Rational& r2) const
{
double r1Val = ((double)numerator)/denominator;
double r2Val =
((double)r2.numerator)/r2.denominator;
return (r1Val > r2Val);
}
// function that returns true if this rational number >= r2,
else false
bool Rational:: operator>=(const Rational& r2) const
{
double r1Val = ((double)numerator)/denominator;
double r2Val =
((double)r2.numerator)/r2.denominator;
return (r1Val >= r2Val);
}
// function that returns true if this rational number == r2,
else false
bool Rational:: operator==(const Rational& r2) const
{
return((numerator == r2.numerator) &&
(denominator == r2.denominator));
}
// function that returns true if this rational number != r2,
else false
bool Rational:: operator!=(const Rational& r2) const
{
return(!(*this == r2));
}
// Define the output and input operator
// function to output the rational number
ostream& operator<<(ostream& out, const Rational&
rational)
{
out<<rational.numerator<<"/"<<rational.denominator;
return out;
}
// function to take input of the rational number
istream& operator>>(istream& in, Rational&
rational)
{
char ch;
in>>rational.numerator>>ch>>rational.denominator;
return in;
}
// function to print the rational number in the form
numerator/denominator
void Rational:: printRational()
{
if (numerator < 0 && denominator < 0) {
setNumerator(numerator * -1);
setDenominator(denominator * -1);
} else if (denominator < 0) {
setNumerator(numerator * -1);
setDenominator(denominator * -1);
}
if (denominator == 0)
cout << "\nDivide by zero error!!!\n";
else if (numerator == 0)
cout << 0;
else
cout << numerator << "/" << denominator;
}
// function to print the rational number in the form of decimal
number obtained when numerator is divided by denominator
void Rational:: printFloating()
{
cout << static_cast < double > (numerator)
/ denominator;
}
//end of Rational.cpp
// main.cpp : C++ client function to test the Rational class
functions
#include <iostream>
#include"Rational.h"
using namespace std;
int main() {
// test the class functions
Rational num1, num2, result;
// get the values from the user for num1
cout<<"Enter first Rational number (in format
numerator/denominator) : ";
cin>>num1;
// get the values from the user for num2
cout<<"Enter second Rational number (in format
numerator/denominator) : ";
cin>>num2;
cout<<"Your first rational number is:
"<<num1<<endl;
cout<<"Your second rational number is:
"<<num2<<endl;
// perform operations and display the result
result = num1 + num2;
cout<<num1<<" + "<<num2<<" =
"<<result<<" (using \"+\" operator) "<<endl;
result = num1 - num2;
cout<<num1<<" - "<<num2<<" =
"<<result<<" (using \"-\" operator) "<<endl;
result = num1 * num2;
cout<<num1<<" * "<<num2<<" =
"<<result<<" (using \"*\" operator) "<<endl;
result = num1 / num2;
cout<<num1<<" / "<<num2<<" =
"<<result<<" (using \"/\" operator) "<<endl;
cout<<num1<<" <
"<<num2<<" is "<<boolalpha<<(num1 <
num2)<<" (using \"/<\" operator) "<<endl;
cout<<num1<<" > "<<num2<<"
is "<<boolalpha<<(num1 > num2)<<" (using
\"/>\" operator) "<<endl;
cout<<num1<<" <= "<<num2<<"
is "<<boolalpha<<(num1 <= num2)<<" (using
\"/<=\" operator) "<<endl;
cout<<num1<<" >= "<<num2<<"
is "<<boolalpha<<(num1 >= num2)<<" (using
\"/>=\" operator) "<<endl;
cout<<num1<<" == "<<num2<<" is
"<<boolalpha<<(num1 == num2)<<" (using \"/==\"
operator) "<<endl;
cout<<num1<<" != "<<num2<<" is
"<<boolalpha<<(num1 != num2)<<" (using \"/!=\"
operator) "<<endl;
return 0;
}
//end of main.cpp
Output:

C++ I am using visual studio for it. Make a copy of the Rational class you...
13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...
Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...
NEED ASAP PLEASE HELP
Task:
---------------------------------------------------------------------------------------------------------------------------------
Tasks to complete:
----------------------------------------------------------------------------------------------------------------------------------------
given code:
-----------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include <iostream>
#include <iomanip>
#include "fractionType.h"
using namespace std;
int main()
{
fractionType num1(5, 6);
fractionType num2;
fractionType num3;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << "Num1 = " << num1 << endl;
cout << "Num2 = " << num2 << endl;
cout << "Enter the fraction in the form a / b: ";
cin >> num2;
cout << endl;
cout <<...
Header file for the Rational class:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function
addition
Rational subtraction( const Rational & ) const; // function
subtraction
Rational multiplication( const Rational & ) const; // function
multi.
Rational division( const Rational & ) const; // function
division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as...
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...
SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){ int num1,num2; cout << "Enter two numbers "<< endl; cout << "First :"; cin >> num1; cout << "Second :"; cin >>num2; int result=num1+num2; cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result; ...
Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....
c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...
If you have already answered this question, please do
not repost your old solutions, you will be thumbs downed. I'm
looking for NEW solutions only!
Frac.h:
// a Fraction object holds one Fraction number, one
fraction
#ifndef FRAC_H
#define FRAC_H
#include <iostream>
using namespace std;
//Creaing a Fraction class
class Fraction {
public:
Fraction(int = 0, int = 1);
// Function Declarations which performs operations on Fraction
class
Fraction add(const Fraction &);
Fraction subtract(const Fraction& a);
Fraction multiply(const Fraction& a);...