Question
C++ only
I'm using code lite editor as well
Please add comments and pre and post conditions too
9:16 AM .oooo Verizon GI 98% Use C++ only Please include comments and pre post conditions am also using a new editor called code lite write a class for rational numbers. Each 15 object in the class should have two inte values that define the rational number: the numerator and the denominator. For example, the fraction 516 would have a denominator of 5 and a nu- merator of 6. Include a constructor with two argu- ments that can be used to set the numerator and roforbidding zero in the denominator. ber fo the denominator. zero for the numerator and one verload the and Nu to be read and written in the form 12/15, 300 and so forth. that the numer. ator, the deno both may contain a minus and 1 are possible. Include a function to normalize the values stored so that, after normalization, is after and as small as possible. For example, same normalization, 4 would be represented the overload the usual arithmetic operators to pro- vide addition, subtraction, multiplication, and divi- sion of two rational numbers. overload the usual comparison operations to allow comparison of two rational numbers. cld are Hints: Two rational numbers alb and equal if a d equals c b. For positive rational num- bers, alb is less than cld, provided a d is less than
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the below class which can be overload mathematical operations on rational numbers.

Here , we explained with comments , how the flow is working in important places . please understnad and use the concepts for any other queries if required .

#include<stdio.h>   
#include<iostream.h>
#include<conio.h>
class rational_number
{
   int numerator1;   //numerator
   int denominator1;   //denominator
   public:
      void getdata()   //this function will use to get both values to makeup rational number
      {
          cout<<"\n Enter the numerator value of the rational no.";
          cin>>numerator1;
          cout<<"\n Enter the denominator value of the rational no.";
          cin>>denominator1;
   }
  
   //if we take an example value , numerator is 2, denominator is 3 , then the rational number will be 2/3
  
   void operator+(rational_number);   //declaration function to add 2 rational numbers by using overload concept
   void operator-(rational_number);   //declaration function to subtracting 2 rational numbers by using overload concept
   void operator*(rational_number);   //declaration function to multiply 2 rational numbers by using overload concept
   void operator/(rational_number);   //declaration function to devided 2 rational numbers by using overload concept
};
void rational_number ::operator+(rational_number c1)       //this is function definition for rational numbers addition
{
   rational_number result1;               //temp is obeject of rational class, then we can access all variables and functions of rational class in this function definition
   result1.numerator1=(numerator1*c1.denominator1)+(c1.numerator1*denominator1);   //rational number are cross multiplying
   result1.denominator1=denominator1*c1.denominator1;          
   cout<<"\n Rational number. after addition";
   cout<<"\n Numerator is ="<<result1.numerator1;
   cout<<"\n Denominator is ="<<result1.denominator1;  
}
void raional_number ::operator -(rational_number c2)       //this is function definition for rational numbers subtraction
{
   rational_number result2;
   result2.numerator1=(numerator1*c2.denominator1)-(c2.numerator1*denominator1);   //rational number are cross multiplying
   result2.denominator1=denominator1*c2.denominator1;
   cout<<"\n rational no. after subtraction";
   cout<<"\n numerator is="<<result2.numerator1;
   cout<<"\n denominator is ="<<result2.denominator1;
}
void rational_number ::operator (rational_number c3)       //this is function definition for rational numbers multiply
{
   rational_number result3;
   result3.numerator1=numerator1*c3.numerator1;
   result3.denominator1=denominator1*c3.denominator1;
   cout<<"\n rational no. after multiplication";
   cout<<"\n numerator="<result3.numerator1;
   cout<<"\n denominator ="<< result3.denominator1;
}
void rational_number :: operator /(rational_number c1)   //this is function definition for rational numbers deviding
{
   rational_number result4;
   result4.numerator1= numerator1*c1.denominator1;
   result4.denominator1=c1.numerator1*denominator1;
   cout<<"\n rational no. after dividation";
   cout<<"\n numerator="<<result4.numerator1
   cout<<"\n denominator ="<<result4.denominator1;
}
void main()
{
   clrscr();
   rational_number a1, a2;
   int ch;
   //we are using do while cocept, to give option for particular operation while running this program
   do
   {
       cout<<"\n 1. To Give Input for rational numbers. ";
       cout<<"\n 2. Addition of rational numbers. ";
       cout<<"\n 3. Subtraction of rational numbers. ";
       cout<<"\n 4. Multiplication of rational numbers.";
       cout<<"\n 5. Division of rational numbers. ";
       cout<<"\n 6. Quit";
       cout<<"\n Enter your choice";
       cin>>ch;   //will ask at runtime to enter choice (ch value)
       switch(ch) //specific case will execte based on entered value
       {
           case 1:
           cout<<endl<<"\n enter the data for first rational number.";
           a1.getdata();
           cout<<endl<<"\n enter the data for second rational number. ";
           a2.getdata ();  
           clrscr();
           break;
           case 2;
           a1+a2;       //it will call to operator+(rational_number); , nothing but "void rational_number ::operator+(rational_number c1)"
           getch();
           clrscr();
           break;
           case 3;
           a1-a2;       //it will call to operator-(rational_number); , nothing but "void rational_number ::operator-(rational_number c1)"
           getch();
           clrscr();
           case 4:
           a1*a2;       //it will call to operator*(rational_number); , nothing but "void rational_number ::operator(rational_number c1)"
           getch();
           clrscr();
           break;
           case 5:
           a1/a2;       //it will call to operator/(rational_number); , nothing but "void rational_number ::operator/(rational_number c1)"
           getch();
           clrscr();
           break;
           case 6:   //this case to exit from the program
           exit(1);
           break;
       }
   } while (ch!=6); //program will not exit till we give 6 as choice
   getch(); //to wiat for our input (with any key) to come out from the running session
}

Add a comment
Know the answer?
Add Answer to:
C++ only I'm using code lite editor as well Please add comments and pre and post...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT