Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in c++ that will utilize a Fraction struct that has a numerator

ID: 3787421 • Letter: W

Question

Write a program in c++ that will utilize a Fraction struct that has a numerator and a denominator. Make an overloaded operator + for the struct that can add two Fractions and return a Fraction result. Make an overloaded * that can also multiply two Fractions together. Make an overloaded operator > that can compare two Fractions and determine whether the first one is greater than the second one. Make another overloaded operator * that has a Fraction as a first operand and an integer as a second operand, which together produce a Fraction result, and a third overloaded operator * which has an integer as a first operand and a Fraction as a second operand.write your code in an object oriented manner.

Explanation / Answer

#include <iostream>
using namespace std;

struct Fraction     // structure definition
{
private:
int numerator;
int denominator;
public:
Fraction() //default constructor
{
this->numerator =0;
this->denominator = 0;
}
Fraction(int numerator,int denominator) //parameterized constructor
{
this->numerator=numerator;
this->denominator=denominator;

}


Fraction operator+(Fraction f1)    //operator function
{
Fraction f;
f.denominator = this->denominator * f1.denominator;
f.numerator = (this->denominator * f1.numerator) + (f1.denominator * this->numerator);

return f;
}


Fraction operator *(Fraction f1)
{

Fraction f;
f.numerator = this->numerator * f1.numerator;
f.denominator = this->denominator * f1.denominator;

return f;
}

Fraction operator *(int i)
{

Fraction f;
f.numerator = this->numerator * i;
f.denominator = this->denominator ;

return f;
}

friend Fraction operator *(int i,Fraction f)   //friend function
{
   Fraction temp;
temp.numerator = f.numerator * i;
temp.denominator = f.denominator;
return temp;
}


bool operator>(Fraction &f2)          //operator function   
{
return ( (this->numerator > f2.numerator && this->denominator == f2.denominator) || (this->denominator < f2.denominator && this->numerator == f2.denominator));
}


void display() //display in fraction
{
cout<<numerator<<"/"<<denominator;
}


};

int main()
{
   Fraction f1(3,5);
   Fraction f2(7,6);
   Fraction result1,result2,result3,result4;
   cout<<"f1 : ";
   f1.display();
   cout<<" f2 : ";
   f2.display();

   cout<<" Adding f1 and f2 : ";
   result1 = f1+f2;
   result1.display();

   cout<<" Multiplying f1 and f2 : ";
   result3=f1*f2;
   result3.display();

   cout<<" Multiplying f1 and 4 : ";
   result3=f1*4;
   result3.display();

   cout<<" Multiplying f2 and 5 : ";
   result3=5*f2;
   result3.display();
   if(f1 > f2)
   cout<<" f1 is greater than f2";
   else
   cout<<" f1 is smaller than f2";




   return 0;
}


output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote