In: Computer Science
Write a Fraction Class in C++
PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE
Thank in Advance.
Must do;
The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor
You must overload the assignment operator
You must declare the overloaded output operator as a friend rather than part of the class
The Fraction class should implement addition, subtraction, multiplication, division and simplify operations.
Write a main function in a separate file to test each functionality and show that they work.
fraction.h
#ifndef H_FRACTION
#define H_FRACTION
#include <iostream>
using namespace std;
//Fraction class
class Fraction
{
//private members
private:
//nm for numerator and dm for denominator
int nm, dm;
//public members
public:
Fraction();
Fraction(int n,int d);
Fraction(const Fraction& fx);
void operator=(Fraction fx);
Fraction operator+(Fraction fx);
Fraction operator-(Fraction fx);
Fraction operator*(Fraction fx);
Fraction operator/(Fraction fx);
void simplify();
friend ostream&
operator<<(ostream& os,const Fraction& ob);
friend istream&
operator>>(istream& is,Fraction& ob);
};
#endif
fraction.cpp
#include <iostream>
#include<math.h>>
#include"fraction.h"
using namespace std;
//default constructor
Fraction::Fraction()
{
nm = 0;
dm = 1;
}
//parameterized constructor
Fraction::Fraction(int n, int d)
{
nm = n;
dm = d;
}
//copy constructor
Fraction::Fraction(const Fraction& t)
{
nm = t.nm;
dm = t.dm;
}
//= operator overloading
void Fraction::operator=(Fraction fx)
{
nm = fx.nm;
dm = fx.dm;
}
//+ operator overloading
Fraction Fraction::operator+(Fraction fx)
{
int k, j;
k=fx.dm *dm;
j=fx.nm * dm + fx.dm * nm;
Fraction tx(j,k);
tx.simplify();
return tx;
}
//- operator overloading
Fraction Fraction::operator-(Fraction fx)
{
int j, k;
k=fx.dm *dm;
j=fx.nm * dm - fx.dm * nm;
Fraction tx(j,k);
tx.simplify();
return tx;
}
//* operator overloading
Fraction Fraction::operator*(Fraction fx)
{
int j, k;
j = fx.nm * nm;
k = fx.dm *dm;
Fraction tx(j,k);
tx.simplify();
return tx;
}
// / operator overloading
Fraction Fraction::operator/(Fraction fx)
{
int j, k;
j = fx.dm * nm;
k = fx.nm * dm;
Fraction tx(j,k);
tx.simplify();
return tx;
}
// << operator overloading
ostream& operator<<(ostream& os,const Fraction&
ob)
{
os<<ob.nm<<"/"<<ob.dm<<"\n";
return os;
}
// >> operator overloading
istream& operator>>(istream& is, Fraction&
ob)
{
is>>ob.nm>>ob.dm;
return is;
}
//function to simplify the fraction
void Fraction::simplify()
{
int i, j, k;
j = abs(nm);
k = abs(dm);
//calculate the gcd
for(i=j; j%i!=0 || k%i!=0; i--);
nm = nm/i;
dm = dm/i;
if(nm<0 && dm<0 ||dm<0)
{
nm = -nm;
dm = -dm;
}
}
main.cpp
#include <iostream>
#include"fraction.h"
using namespace std;
//main function
int main()
{
Fraction f1(2,3), f2(1,3), f3, f4;
cout<<"f1 = "<<f1<<endl;
cout<<"f2 = "<<f2<<endl;
f3=f1+f2;
cout<<"Result of addition: "<<f3<<endl;
f3=f1-f2;
cout<<"Result of subtraction: "<<f3<<endl;
f4=f1*f2;
cout<<"Result of multiplication:
"<<f4<<endl;
f4=f1/f2;
cout<<"Result of division: "<<f4<<endl;
return 0;
}
Output:
f1 = 2/3
f2 = 1/3
Result of addition: 1/1
Result of subtraction: -1/3
Result of multiplication: 2/9
Result of division: 2/1