In: Computer Science
C++ file=.class definitions.h
Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member function called reduce() that gets called after arithmetic operations to reduce the frraction. +, -, *, / must return the result of the operation; e.g.: the sum or difference of the two fractions. +=, -=, *= and /= must assign the result of the operation to the object that's invoking the operation; i.e.: frac1 += frac2 must modify frac1 to make it equal to the sum of the two fractions, but frac1 + frac2 must simply return the sum.
If n1/d1 and n2/d2 are two fractions, their sum fraction's numertor is: n1 * d2 + n2 * d1 and its denominator is d1 * d2. To compare the two fractions, you can compare n1 * d2 with n2 * d1.
ANSWER:
#include <iostream>
using namespace std;
class Fractions { // The class
private: // Access specifier
int num; // Attribute (int variable)
int den;
Fractions reduce(int r)
{
Fractions t;
t.num=num-r*den;
t.den=den;
cout<<"reduced fraction is:
"<<t.num<<"/"<<t.den<<"\n";
cout<<"___________________________________\n";
return t;
}
public:
Fractions()
{
num=0;
den=1;
}
Fractions(int nn,int dd)
{
num=nn;
den=dd;
}
~Fractions()
{
}
Fractions operator=(Fractions f)
{
//cout<<"Assignment operator invoked\n";
// cout<<"_____________________________\n";
num=f.num;
den=f.den;
return Fractions(num,den);
}
Fractions (const Fractions& f)
{
// cout<<"Copy constructor invoked\n";
// cout<<"_________________________\n";
num=f.num;
den=f.den;
}
Fractions decrement(int i)
{
reduce(i);
}
Fractions operator+= (Fractions f)
{
num=num*f.den+f.num*den;
den=den*f.den;
cout<<"new value of the fraction after += operation is :"<<num<<"/"<<den<<"\n";
}
Fractions operator-=(Fractions f)
{
num=num*f.den-f.num*den;
den=den*f.den;
cout<<"new value of the fraction after -= operation is
:"<<num<<"/"<<den<<"\n";
}
Fractions operator*=(Fractions f)
{
num=num*f.num;
den=den*f.den;
cout<<"new value of the fraction after *= operation is
:"<<num<<"/"<<den<<"\n";
}
Fractions operator/=(Fractions f)
{
num=num*f.den;
den=den*f.num;
cout<<"new value of the fraction after /= operation is
:"<<num<<"/"<<den<<"\n";
}
Fractions operator +(Fractions F2)
{
Fractions t;
t.num=num*F2.den+F2.num*den;
t.den=den*F2.den;
return t;
}
Fractions operator-(Fractions F2)
{
Fractions t;
t.num=num*F2.den-F2.num*den;
t.den=den*F2.den;
return t;
}
Fractions operator*(Fractions F2)
{
Fractions t;
t.num=num*F2.num;
t.den=den*F2.den;
return t;
}
Fractions operator/(Fractions F2)
{
Fractions t;
t.num=num*F2.den;
t.den=den*F2.num;
return t;
}
bool operator==(Fractions F2)
{
bool x;
if(num*F2.den==F2.num*den)
x=1;
else
x=0;
return x;
}
bool operator<(Fractions F2)
{
bool x;
if(num*F2.den<F2.num*den)
x=1;
else
x=0;
return x;
}
bool operator>(Fractions F2)
{
bool x;
if(num*F2.den>F2.num*den)
x=1;
else
x=0;
return x;
}
int operator!=(Fractions F2)
{
bool x;
if(num*F2.den!=F2.num*den)
x=1;
else
x=0;
return x;
}
friend ostream& operator<<(ostream&
s,Fractions& f);
friend istream& operator>>(istream& s,Fractions&
f);
};
ostream& operator<<(ostream& s,Fractions&
f)
{
s<<"("<<f.num<<"/"<<f.den<<")";
return s;
}
istream& operator>>(istream& s,Fractions&
f)
{
s>>f.num>>f.den;
return s;
}
int main() {
Fractions f1(1,5); // Create an object of Fractions
Fractions f2,f4;//two othe objects without innitialising
values
f4=f2=f1;//initialising objects using assignment operator
Fractions f3=f1;//Initialising using copy constructor
f1.decrement(3);
f4+=f2;
f4-=f1;
f2*=f3;
f1/=f3;
Fractions f5;
f5=f1+f2;
f5=f1-f2;
f5=f1*f2;
f5=f1/f2;
Fractions f6;
if(f1!=f4)f6=f1;
else f6=f4;
Fractions f7;
if(f1<f4)f7=f1;
else f7=f4;
cout<<"Friend function checking:f1="<<f1<<
"\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f2="<<f2<<
"\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f3="<<f3<<
"\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f4="<<f4<<
"\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f5="<<f5<<
"\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f6="<<f6<<
"\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f7="<<f7<<
"\n";
cout<<"---------------------------\n";
return 0;
}