Write a rational number class For now we will use member fun

Write a rational number class. For now, we will use member functions add, sub, mul, div and less that
each carry out the operations +, -, *, /, and <. For example , a+b will be written as a.add(b), and a<b will
be written a.less(b).
The rational number is composed of two integers with division indicated as 2/3, 15/32. 65/4; the
division is not carried out, it is only indicated. So in the rational number is represented by two int values,
numerator and denominator.
The constructor is to create objects with any legal value, so the constructor will have two it parameters.
Since every int is also a rational number, like 2/1 or 9/1, you should have a constructor with a single int
parameter.
Member functions add, sub, mul, div will return a rational value; function less will return a bool value.
These functions should do the operation suggested by the name.
Write a main function that will thoroughly tests your class implementation.
The following formulas will be useful in defining functions:
a/b + c/d = (a*d +b*c)/b*d
a/b - c/d = (a*d - b*c)/b*d
a/b * c/d =(a*c) / (b*d)
(a/b) / (c/d) = (a*d) /(c*b)
(a/b><(c/d) means (a*d) <(c*b)
Let any sign be carried by the numerator, keep the denominator positive.

Solution

//Rational.h
//Rational header file declaration
#ifndef Rational_H
#define Rational_H
#include<iostream>
using namespace std;
//Rational class defintion
class Rational
{
public:

   Rational(int numerator, int denominator);//construtor
   Rational();//defacult constuctor
  
   //member functions of class Rational
   Rational add(Rational &r); //for addition
   Rational sub(Rational &r);//for subtraction
   Rational mul(Rational &r);//for multiplication
   Rational div(Rational &r);//for division
   int less(Rational &r); //for checking less than rational value of passing argument, Rational r

   //To read and output the rational object numerator and denominator values
   void input();
   void output();  

private:
   //private members of Rational class
   int numerator, denominator;
  
};

#endif Rational_H


-------------------------------------------------------------------------------------------------
//Rational.cpp
#include<iostream>
#include \"Rational.h\"
using namespace std;
//Constructor to set numerator and denominator to default to 0 and 1
//for numerator and denominator
Rational::Rational()
{
   numerator = 0;
   denominator = 1;
}
//Parameterized constructor
Rational::Rational(int num , int den)
{
   numerator = num;
   denominator = den;
}
//All methods of Rational class add,sub ,div and mul that accepts teh Rational object as input argumnts
//Addition of two fractions
Rational Rational::add(Rational &r)
{
   Rational result;
   int n= numerator*r.denominator+denominator*r.numerator;
   result.numerator=n;
   int d = denominator*r.denominator;
   result.denominator=d;

   return Rational(result.numerator,result.denominator);
}
//Subtractions of two fractions
Rational Rational::sub(Rational &r)
{
   Rational result;
   int n= numerator*r.denominator - numerator*r.denominator;
   result.numerator=n;
   int d = denominator*r.denominator;
   result.denominator=d;
   return Rational(result.numerator,result.denominator);
}

//Divsion of two fractions
Rational Rational::div(Rational &r)
{
   Rational result;
   int n= numerator*r.denominator;
   result.numerator=n;
   int d = denominator*r.numerator;
   result.denominator=d;

   return Rational(result.numerator,result.denominator);
}

//Multiplication of two fractions
Rational Rational::mul(Rational &r)
{
   Rational result;
   int n= numerator*r.numerator;
   result.numerator=n;
   int d = denominator*r.denominator;
   result.denominator=d;

   return Rational(result.numerator,result.denominator);
}

//Returns 1 if rational , r is less than the Rational class object
int Rational::less(Rational &r)
{
   if((numerator/denominator)<(r.numerator/r.denominator))
       return 1;
   else
       return 0;
}

//The method input that reads two integer for numeator and denominator
void Rational::input()
{
  
   //to read a operator character
   //and then ignore ignore the operator
   char ch;  
   cin>>numerator;
   cin>>ch;
   cin>>denominator;  
}
//The method output that prints the rational number in numerator and denominator
void Rational::output()
{
   cout<<numerator<<\" / \"<<denominator<<\" \";
}

-------------------------------------------------------------------------------------------------------------
//Driver program

/*C++ test program for Rational class methods add,sub,mul ,div and input and output
methods and prints the corresponding resutls to the console screen*/

//main.cpp
#include<iostream>
//include Rational header file
#include \"Rational.h\"
using namespace std;
int main(void)
{

   //Create two objects r1,r2 of Rational class
   Rational r1,r2;
   cout<<\"Enter two rational numbers : \"<<endl;
   //read rational number in r1
   cout<<\"Enter rational number (num/den): \";
   r1.input();
   //read rational number in r2
   cout<<\"Enter rational number (num/den): \";
   r2.input();

   //add two fractions
   Rational addition = r1.add(r2);
   r1.output();
   cout<<\"+\";
   r2.output();
   cout<<\"=\";
   addition.output();

   cout<<endl;

   //Subtract two fractions
   Rational subtraction = r1.sub(r2);
   r1.output();
   cout<<\"-\";
   r2.output();
   cout<<\"=\";
   subtraction.output();

   cout<<endl;

   //Multiplication of two fractions
   Rational multiplication = r1.mul(r2);
   r1.output();
   cout<<\"*\";
   r2.output();
   cout<<\"=\";
   multiplication.output();

   cout<<endl;

   //devision of two fractions
   Rational division = r1.div(r2);
   r1.output();
   cout<<\"/\";
   r2.output();
   cout<<\"=\";
   division.output();

   cout<<endl;

   //calling less method on two rational objects r1 and r2
   if(r1.less(r2))
   {
       r1.output();
       cout<<\" is less than \";
       r2.output();
       cout<<endl;
   }
   else
   {
       r1.output();
       cout<<\" is not less than \";
       r2.output();
       cout<<endl;
   }

   //pause the program output
   system(\"pause\");
   return 0;
}

Write a rational number class. For now, we will use member functions add, sub, mul, div and less that each carry out the operations +, -, *, /, and <. For ex
Write a rational number class. For now, we will use member functions add, sub, mul, div and less that each carry out the operations +, -, *, /, and <. For ex
Write a rational number class. For now, we will use member functions add, sub, mul, div and less that each carry out the operations +, -, *, /, and <. For ex
Write a rational number class. For now, we will use member functions add, sub, mul, div and less that each carry out the operations +, -, *, /, and <. For ex

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site