RationalNumber.h
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
class RationalNumber
{
public:
RationalNumber();
// Initializes the rational number to 0
RationalNumber(int wholeNumber);
// Initializes the rational number to wholeNumber/1
RationalNumber(int m, int n);
// Initializes the rational number to m/n if n is not 0;
// the sign of the rational is stored in the numerator
// (the denominator is always positive);
// exits if n = 0 (invalid rational number)
void output();
// Precondition: The rational number is defined.
// Postcondition: The rational number has been displayed on
// the screen in the form m/n.
friend bool operator ==(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 equals r2; false otherwise.
friend bool operator <(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than r2; false otherwise.
friend bool operator >(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than r2; false otherwise.
friend bool operator <=(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than or equal to r2; false otherwise.
friend bool operator >=(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than or equal to r2; false otherwise.
friend bool operator !=(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is not equal to r2; false otherwise.
friend RationalNumber operator+(RationalNumber a, RationalNumber
b);
friend RationalNumber operator-(RationalNumber a, RationalNumber b);
friend RationalNumber operator*(RationalNumber a, RationalNumber b);
friend RationalNumber operator/(RationalNumber a, RationalNumber b);
void printRational();
int gcd(int a, int b);
private:
// Declaring variables
int numer; // the numerator of the number
int denom; // the denominator of the number
};
#endif
________________
RationalNumber.cpp
#include <iostream>
using namespace std;
#include "RationalNumber.h"
// Initializes the rational number to 0
RationalNumber::RationalNumber()
{
this->numer = 0;
this->denom = 1;
}
// Initializes the rational number to wholeNumber/1
RationalNumber::RationalNumber(int wholeNumber)
{
this->numer=wholeNumber;
this->denom=1;
}
// Initializes the rational number to m/n if n is not 0;
// the sign of the rational is stored in the numerator
// (the denominator is always positive);
// exits if n = 0 (invalid rational number)
RationalNumber::RationalNumber(int m, int n)
{
if(n==0)
{
cout<<"** Denominator Must nor be zero **"<<endl;
exit(0);
}
else if(denom<0)
{
this->numer=-(m);
this->denom=-(n);
}
else
{
this->numer=(m);
this->denom=(n);
}
}
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 equals r2; false otherwise.
bool operator ==(const RationalNumber& r1, const
RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;
}
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than r2; false otherwise.
bool operator <(const RationalNumber& r1, const
RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;
}
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than r2; false otherwise.
bool operator >(const RationalNumber& r1, const
RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;
}
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than or equal to r2; false
otherwise.
bool operator <=(const RationalNumber& r1, const
RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;
}
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than or equal to r2; false
otherwise.
bool operator >=(const RationalNumber& r1, const
RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;
}
bool operator !=(const RationalNumber& r1, const
RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;
}
RationalNumber operator+(RationalNumber r1, RationalNumber r2) {
int a, b, c, d;
a =r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int sumnumer = (a * d + b * c);
int sumdenom = (b * d);
RationalNumber r(sumnumer, sumdenom);
return r;
}
RationalNumber operator-(RationalNumber r1, RationalNumber r2) {
int a, b, c, d;
a = r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int subnumer = (a * d - b * c);
int subdenom = (b * d);
RationalNumber r(subnumer, subdenom);
return r;
}
RationalNumber operator*(RationalNumber a, RationalNumber b) {
RationalNumber result((a.numer*b.numer) , (a.denom*b.denom));
return result;
}
RationalNumber operator/(RationalNumber a, RationalNumber b) {
RationalNumber result((a.numer*b.denom) , (a.denom*b.numer));
return result;
}
void RationalNumber::printRational()
{
int g=gcd(numer,denom);
if(denom!=1)
cout<<numer/g<<"/"<<denom/g;
else
cout<<numer/g;
}
int RationalNumber::gcd(int a, int b)
{
// % is modulus which is the remainder of a division
// base case
if ((a % b) == 0)
{
return b;
}
// recursive case
else
{
return gcd(b, a % b);
}
}
_____________________
main.cpp
#include <iostream>
using namespace std;
#include "RationalNumber.h"
int main()
{
RationalNumber c( 7, 3 ), d( 3, 9 ), x;
c.printRational();
cout << " + " ;
d.printRational();
cout << " = ";
x = c + d; // test overloaded operators + and =
x.printRational();
cout << '\n';
c.printRational();
cout << " - " ;
d.printRational();
cout << " = ";
x = c - d; // test overloaded operators - and =
x.printRational();
cout << '\n';
c.printRational();
cout << " * " ;
d.printRational();
cout << " = ";
x = c * d; // test overloaded operators * and =
x.printRational();
cout << '\n';
c.printRational();
cout << " / " ;
d.printRational();
cout << " = ";
x = c / d; // test overloaded operators / and =
x.printRational();
cout << '\n';
c.printRational();
cout << " is:\n";
// test overloaded greater than operator
cout << ( ( c > d ) ? " > " : " <= " );
d.printRational();
cout << " according to the overloaded > operator\n";
// test overloaded less than operator
cout << ( ( c < d ) ? " < " : " >= " );
d.printRational();
cout << " according to the overloaded < operator\n";
// test overloaded greater than or equal to operator
cout << ( ( c >= d ) ? " >= " : " < " );
d.printRational();
cout << " according to the overloaded >= operator\n";
// test overloaded less than or equal to operator
cout << ( ( c <= d ) ? " <= " : " > " );
d.printRational();
cout << " according to the overloaded <= operator\n";
// test overloaded equality operator
cout << ( ( c == d ) ? " == " : " != " );
d.printRational();
cout << " according to the overloaded == operator\n";
// test overloaded inequality operator
cout << ( ( c != d ) ? " != " : " == " );
d.printRational();
cout << " according to the overloaded != operator" << endl;
cin.get();
// system("PAUSE");
return 0;
} // end main
________________
output:

_______________Thank You
Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...
Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different class and functions from problem 1. The main function calls different functions as instructed in the problem description. Submit the CPP file during submission Problem 1. Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and the denominator (one for each data). Also...
C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...
This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...
Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to...
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);...
Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...
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...
(Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...
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...
PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT
VISUAL.
*****PLEASE USE C#(C SHARP)*****
Also please dont use BigInteger
thanks it's really appreciated for all the help
PLEASE MAKE IT SO ITS USER INPUT
Create a class called Rational for prforming arithmetie with fractions. Write an app to test your class Use integer variables to represent the private instance variables of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized...