Rational number class Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two integers with division indicated. Requirement: - two member variables: (int)...

1 answer below »

Rational number class  Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two integers with division indicated.   Requirement: - two member variables: (int) numerator and (int) denominator.  - two constructors: one that takes in both numerator and denominator to construct a rational number, and one that takes in only numerator and initialize the denominator as 1.  - accessor/modifier - member functions: add(), sub(), mul(), div(), and less(). Usage: to add rational num b and rational num a, one could call a.add(b); to compare rational num a and rational num b, one could call a.less(b). add(), sub(), mul() and div() should return a rational number.   - member functon: factor(). This function should be a private function that should be called each time numerator/denominator is set/modified. The purpose of this function is to take the greatest common factor out of numerator/denominator. For example, rational number 2/4 should be stored as 1/2, rational number 3/9 should be stored as 1/3.  - fully test all public functions inside main().   Grading:  - compilable and meaningfull attemps: - class definition: 20% - class implementation: 30% - test and main(): - comment, indentation and file name
Answered Same DayMar 30, 2021

Answer To: Rational number class Define a class for rational numbers. A rational number is a "ratio-nal"...

David answered on Mar 30 2021
141 Votes
#include //Standard Input Output    
#include //Input Output Stream
#include //Console Input Ouptut
class rational

{
     int numerator; //value of numerator
     int denominator; // value of denominator
     int simplifiednum; //value of numerator in simple form
     int simplifieddin; //value of denominator in simple form
    
     //Private method to get simplied form of fraction provided by user
     private:void getgcf(int a, int b)
     {
     int i;
     if (a == 0)
{
std::cout << "Invalid Entry. Cannot divide fraction by Zero." << std::endl;
}

if (b == 0)
{
std::cout << "Invalid Operation. Numerator cannot be zero." << std::endl;
}
for (i = a * b; i > 1; i--)
{
if ((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
simplifiednum=a;
simplifieddin=b;
     }
    
//get value of numerator and denominator from user and get them simplied
     public:void getfactor()
     {
     std::cout<<"\n Enter the numerator(must be greater than 0) part of the rational number- ";
         std::cin>>numerator;
         std::cout<<"\n Enter the denominator(must be greater than 0) part of the rational number- ";
         std::cin>>denominator;
         getgcf(numerator,denominator);
         std::cout<<"\nSimplified Factor "<     }
    
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here