Fraction is the ratio of two natural numbers, denoted by p/q where p is known as numerator and q is known as denominator and is not equal to zero. All the arithmetic operations can be executed in fractions, to implement these functions in C++, a fraction class is defined to handle these operations efficiently. Since, a new data type is created by defining a class, a Fraction data object will behave like any other numeric type when the operations for the Fraction type are executed, and it will perform operations like subtraction, addition, division, and multiplication.
Fraction Class
The fraction Class allows the user to handle the improper fractions, which are in the form of numerator divided by denominator, denoted by p/q where p is numerator and q is denominator and is not equal to zero. Just like the method of operation on basic numeric types, such as int, the arithmetic and comparison operations can also be performed on the objects of Fraction. Same as integers, the I/O works with fractions, the inputs taken in 1/2 or 0.5 form are valid and will be stored in their reduced form.
Example
The example demonstrates the use of Fraction Class. Comparison operation and the arithmetic operations are performed in the program.
#include<cstdlib>
using namespace std;
class Fraction
{
public:
int numerator, denominator;
public:
Fraction()
{
numerator = 1;
denominator = 1;
}
Fraction(int n, int d)
{
numerator = n;
if (d==0)
{
cout << "Error: Attempting to Divide by Zero" << endl;
exit(0); // it will terminate the program if division by 0 is attempted
}
else
denominator = d;
}
Fraction operator +(Fraction f)
{
int n = numerator*f.denominator+f.numerator*denominator;
int d = denominator*f.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator -(Fraction f)
{
int n = numerator*f.denominator-f.numerator*denominator;
int d = denominator*f.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator *(Fraction f)
{
int n = numerator*f.numerator;
int d = denominator*f.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator /(Fraction f)
{
int n = numerator*f.denominator;
int d = denominator*f.numerator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
bool operator == (Fraction &f)
{
return (numerator==f.numerator) && (denominator==f.denominator);
}
int gcd(int n, int d)
{
int rem;
while (d != 0)
{
rem = n % d;
n = d;
d = rem;
}
return n;
}
void accept()
{
cout<<"\n Enter the value of Numerator : ";
cin>>numerator;
cout<<"\n Enter the value of Denominator : ";
cin>>denominator;
}
};
int main()
{
Fraction f1;
Fraction f2;
Fraction f3;
cout<<"\n Enter 1st Fraction Value ";
f1.accept();
cout<<"\n Enter 2nd Fraction Value ";
f2.accept();
f3=f1+f2;
cout<<"\n Sum of Two Fractions : "<<f3.numerator<<"/"<<f3.denominator<<endl;
f3=f1-f2;
cout<<"\n Difference of Two Fractions : "<<f3.numerator<<"/"<<f3.denominator<<endl;
f3=f1*f2;
cout<<"\n Product of Two Fractions : "<<f3.numerator<<"/"<<f3.denominator<<endl;
f3=f1/f2;
cout<<"\n Division of Two Fractions : "<<f3.numerator<<"/"<<f3.denominator;
if(f1 == f2)
cout<<"\n Fraction 1 is Equal to Fraction 2"<<endl;
else
cout<<"\n Fraction 1 is Not Equal to Fraction 2"<<endl;
return 0;
}
In this program, the Fraction Class is defined, and the user inputs the values for both fractions. The check operation is used to check if the denominator is zero or not, in case of zero denominator the error will be returned, and the program will terminate otherwise it will jump onto the next function. The GCD function refers to the Greatest Common Divisor function, for two numbers it accepts both numerator and denominator values and returns the greatest common divisor of both the integers. The arithmetic operations are performed on the fractions input by the user. This program is designed to operate on the two fractions, which can be either the same or different.
The user inputs the two fractions on which arithmetic operations are executed, and the results are returned. In the end, these fractions are compared to check whether they are same or different. In this case, both are different.
Conclusion
The Fraction Class allows the user to handle the improper fractions, which are in the form of numerator divided by denominator, denoted as p/q where p is numerator and q is denominator. The comparison and arithmetic operations can be performed on the Fraction Class.