**Rational Operations using Operator Overloading**

**Rational Operations using Operator Overloading**

Write a C++ Program for Rational Operations using Operator Overloading. Here’s a Simple Program to perform Rational Number Arithmetic Operations using Operator Overloading in C++ Programming Language.

*What is Overloading in C++ ?*

*What is Overloading in C++ ?*

C++ allows you to specify more than one definition for a **function** name or an **operator** in the same scope, which is called **function overloading** and **operator overloading** respectively.

**Function overloading : :**

**Function overloading : :**

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.

**Operators overloading : :**

**Operators overloading : :**

You can redefine or overload most of the built-in operators available in C++. Thus a programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

Below is the source code for C++ Program for Rational Operations using Operator Overloading which is successfully compiled and run on Windows System to produce desired output as shown below :

**SOURCE CODE : :**

**SOURCE CODE : :**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/* C++ Program for Rational Operations using Operator Overloading */ #include<stdio.h> #include<iostream> using namespace std; class rational { int numer; int denom; public: void getdata() { cout<<"\n enter the numerator part of the rational no. :: "; cin>>numer; cout<<"\n enter the denominator part of the rational no. :: "; cin>>denom; } void operator+(rational); void operator-(rational); void operator *(rational); void operator /(rational); }; void rational ::operator+(rational c1) { rational temp; temp.numer=(numer*c1.denom)+(c1.numer*denom); temp.denom=denom*c1.denom; cout<<"\nrational no. after addition :: "; cout<<"\n numerator = "<<temp.numer<<"\n denominator = "<<temp.denom; } void rational ::operator -(rational c1) { rational temp; temp.numer=(numer*c1.denom)-(c1.numer*denom); temp.denom=denom*c1.denom; cout<<"\n rational no. after subtraction :: "; cout<<"\n numerator = " <<temp.numer<<"\n denominator = "<<temp.denom; } void rational ::operator *(rational c1) { rational temp; temp.numer=numer*c1.numer; temp.denom=denom*c1.denom; cout<<"\n rational no. after multiplication :: "; cout <<"\n numerator = "<<temp.numer<<"\n denominator = "<< temp.denom; } void rational :: operator /(rational c1) { rational temp; temp.numer= numer*c1.denom; temp.denom=c1.numer*denom; cout<<"\n rational no. after dividation :: "; cout <<"\n numerator = "<<temp.numer<<"\n denominator = "<<temp.denom; } int main() { rational c1, c2; int n; do { cout<<"\n\n 1.Input data for rational no. "; cout<<"\n 2. Addition of rational no. "; cout<<"\n 3. Subtraction of rational no. "; cout<<"\n 4. Multiplication of rational no."; cout<<"\n 5. Division of rational no. "; cout<<"\n 6. Quit"; cout<<"\n\n Enter your choice :: "; cin>>n; switch(n) { case 1: cout<<endl<<"\n enter the data for first rational no.:: "; c1.getdata(); cout<<endl<<"\n enter the data for second rational no. :: "; c2.getdata (); break; case 2: c1+c2; break; case 3: c1-c2; break; case 4: c1*c2; break; case 5: c1/c2; break; case 6: exit(1); break; } } while (n!=6); return 0; } |

**OUTPUT : :**

**OUTPUT : :**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/* C++ Program for Rational Operations using Operator Overloading */ 1.Input data for rational no. 2. Addition of rational no. 3. Subtraction of rational no. 4. Multiplication of rational no. 5. Division of rational no. 6. Quit Enter your choice :: 1 enter the data for first rational no.:: enter the numerator part of the rational no. :: 3 enter the denominator part of the rational no. :: 7 enter the data for second rational no. :: enter the numerator part of the rational no. :: 2 enter the denominator part of the rational no. :: 9 1.Input data for rational no. 2. Addition of rational no. 3. Subtraction of rational no. 4. Multiplication of rational no. 5. Division of rational no. 6. Quit Enter your choice :: 2 rational no. after addition :: numerator = 41 denominator = 63 1.Input data for rational no. 2. Addition of rational no. 3. Subtraction of rational no. 4. Multiplication of rational no. 5. Division of rational no. 6. Quit Enter your choice :: 3 rational no. after subtraction :: numerator = 13 denominator = 63 1.Input data for rational no. 2. Addition of rational no. 3. Subtraction of rational no. 4. Multiplication of rational no. 5. Division of rational no. 6. Quit Enter your choice :: 4 rational no. after multiplication :: numerator = 6 denominator = 63 1.Input data for rational no. 2. Addition of rational no. 3. Subtraction of rational no. 4. Multiplication of rational no. 5. Division of rational no. 6. Quit Enter your choice :: 5 rational no. after dividation :: numerator = 27 denominator = 14 1.Input data for rational no. 2. Addition of rational no. 3. Subtraction of rational no. 4. Multiplication of rational no. 5. Division of rational no. 6. Quit Enter your choice :: 6 Process returned 1 |

Above is the source code and output for C++ Program for Rational Operations using Operator Overloading which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may * Contact Us* through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.

**Thanks for reading the post….**