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

C++ Define a class for a type called Fraction. This class is used to represent a

ID: 3704861 • Letter: C

Question

C++

Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functionsand allow the user to set the numerator and the denominator. Also include a member function that returns the value of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. For example instead of outputting 20/60 the function should output 1/3. This will require finding the greatest common divisor for the numerator and the denominator, and then dividing both by the number. Embed your class in a test program. Additional information Use only one file for the program. Clearly separate the declaration and implementation sections. Sample output: Please enter the numerator: 6 Please enter the denominator: 12 Your fraction is: 6/12 The value of the fraction is: 0.5 The fraction in lowest terms is: 1/2

Explanation / Answer

Solution:

code:

#include <iostream>
//Define a class for a type called Fraction.
class Fraction
{
//Declare variables
int num;
int den;
public:
//member functions
void setNum(int num);
void setDen(int den);
int getNum();
int getDen();
int gcd(int x,int y);
//mutator functions
Fraction add(Fraction first,Fraction second);
Fraction sub(Fraction first, Fraction second);
Fraction mult(Fraction first, Fraction second);
Fraction div(Fraction first, Fraction second);
Fraction inc(Fraction f);
void print();
void print1();
};
//Include mutator functions that allow the
//user to set the numera- tor and the denominator
void Fraction::setNum(int n)
{
num = n;
}
void Fraction::setDen(int d)
{
den = d;
}
int Fraction::getNum()
{
return num;
}
int Fraction::getDen()
{
return den;
}
int Fraction::gcd(int x, int y)
{
return (x==0)?y:gcd(y%x,x);
}
Fraction Fraction::add(Fraction first, Fraction second)
{
std::cout<<" Sum: ";
Fraction sum;
sum.setNum(first.num*second.den+second.num*first.den);
sum.setDen(first.den*second.den);
return sum;
}
Fraction Fraction::sub(Fraction first, Fraction second)
{
std::cout<<" Subtraction: ";
Fraction dif;
dif.setNum(first.num*second.den-second.num*first.den);
dif.setDen(first.den*second.den);
return dif;
}
Fraction Fraction::mult(Fraction first, Fraction second)
{
std::cout<<" Product: ";
Fraction prod;
prod.setNum(first.num*second.num);
prod.setDen(first.den*second.den);
return prod;
}
Fraction Fraction::div(Fraction first, Fraction second)
{
std::cout<<" Division: ";
Fraction div;
div.setNum(first.num*second.den);
div.setDen(first.den*second.num);
return div;
}
Fraction Fraction::inc(Fraction f)
{
std::cout<<" Increment numerator: ";
f.setNum(f.num+=f.den);
return f;
}
void Fraction::print()
{
std::cout<<getNum()<<"/"<<getDen()<<" ";
}
void Fraction::print1()
{
Fraction f;
f.setNum(getNum()/gcd(getNum(),getDen()));
f.setDen(getDen()/gcd(getNum(),getDen()));
std::cout<<f.getNum()<<"/"<<f.getDen()<<" ";
}
int main()
{
Fraction f1, f2, f3, f4, f5, f6, f7;

f1.setDen(4);
f1.setNum(2);
std::cout<<"Fraction 1: ";
f1.print();
f2.setDen(6);
f2.setNum(3);
std::cout<<"Fraction 2: ";
f2.print();
if(f1.getNum()==0||f2.getNum()==0)
{
  f3 = f3.add(f1,f2);
  f3.print1();
  f4=f4.sub(f1, f2);
  f4.print1();
  f5=f5.mult(f1, f2);
  f5.print();
  f6=f6.div(f1, f2);
  f6.print();
  f7=f7.inc(f1);
  f7.print();
}
else
{
  f3 = f3.add(f1,f2);
  f3.print1();
  f4=f4.sub(f1, f2);
  f4.print1();
  f5=f5.mult(f1, f2);
  f5.print1();
  f6=f6.div(f1, f2);
  f6.print1();
  f7=f7.inc(f1);
  f7.print1();
}
//Pause the system for a while
system("pause");
return 0;
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)