Define a Fraction class similar to the one

Question

Define a Fraction class similar to the one shown in earlier modules 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. Also, include a private member function called reduce() that gets called after arithmetic operations to reduce the fraction. +, -, *, / 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 numerator is: n1 * d2 + n2 * d1 and its denominator is d1 * d2. To compare the two fractions, you can compare n1 * d2 with n2 * d1. Define a class called FracList with private members fracPtr, a Fraction pointer, and size to hold the size of the array of Fraction objects. Include constructor, destructor, copy constructor and overload the assignment, insertion (>>), and extraction (<<) operators.

the [] operators for accessing array elements (both as lvalue and rvalue . see the example given in last module) and relational operators <, >, ==, and !=. For < and >. compare the size of the two FracList objects and for == and != see if they’re identical with identical array elements. Also, include two resize() member functions to resize the list, one keeping the existing values and one without keeping old values Also, include a sort and binary search function.

Explanation

Here in this given question, we Define a Fraction class similar to the one shown in earlier modules with num.  And also we have given operators like comparison and arithmetic. And addition to this we have also a friend function. We also include a private member function called reduce() that gets called after arithmetic operations.

Syntax:
Return_type operator operation_symbol(parameter)
{ //additional task
}

Code

#include<iostream>
#include<algorithm>
using namespace std;

class fraction{	//fraction class
    int num, den;	//private data members
    void reduce()
    {	int d=__gcd(num,den);
        num=num/d;
        den=den/d;
    }
public:
    fraction()	//default constructor
    {	num=0;
        den=1;
    }
    fraction(int a, int b)	//parameterized constructor
    {	num=a;
        den=b;
    }
    fraction(const fraction &f)	//copy constructor
    {	num=f.num;
        den=f.den;
    }
    fraction operator=(const fraction &f)	//overloading operator=
    {	fraction obj;
        obj.num=f.num;
        obj.den=f.den;
        return obj;
    }
    int operator==(const fraction &f)	//overloading operator==
    {	if(f.num==num && f.den==den)
            return 1;
        else
            return 0;
    }
    int operator!=(const fraction &f)	//overloading operator==
    {	if(f.num==num && f.den==den)
            return 0;
        else
            return 1;
    }
    int operator<(const fraction &f)	//overloading operator<
    {
        if(f.num*den < num*f.den)
            return 1;
        else
            return 0;
    }
    int operator>(const fraction &f)	//overloading operator>
    {
        if(f.num*den > num*f.den)
            return 1;
        else
            return 0;
    }
    fraction operator+(fraction &f)	//overloading operator+
    {	fraction obj;
        obj.num=num*f.den + f.num*den;
        obj.den=den*f.den;
        obj.reduce();
        return obj;
    }
    fraction operator-(fraction &f)	//overloading operator -
    {	fraction obj;
        obj.num=num*f.den - f.num*den;
        obj.den=den*f.den;
        obj.reduce();
        return obj;
    }
    fraction operator*(fraction &f)	//overloading operator *
    {	fraction obj;
        obj.num=num*f.num;
        obj.den=den*f.den;
        obj.reduce();
        return obj;
    }	
    fraction operator/(fraction &f)	//overloading operator 
    {	fraction obj;
        obj.num=num*f.den;
        obj.den=den*f.num;
        obj.reduce();
        return obj;
    }
    void operator +=(fraction &f)	//overloading operator +=
    {	num=num*f.den+f.num*den;
        den=den*f.den;
        this->reduce();
    }
    void operator -=(fraction &f)	//overloading operator -=
    {	num=num*f.den-f.num*den;
        den=den*f.den;
        this->reduce();
    }
    void operator *=(fraction &f)	//overloading operator *=
    {	num=num*f.num;
        den=den*f.den;
        this->reduce();
    }
    void operator /=(fraction &f)	//overloading operator /=
    {	num=num*f.den;
        den=den*f.num;
        this->reduce();
    }
    friend ostream & operator << (ostream &out, const fraction &c);
    friend istream & operator >> (istream &in,  fraction &c);
    
    ~fraction()
    {	cout<<"\nDestructor called";
    }	//destructor for class
};
ostream & operator << (ostream &out, const fraction &f)
{	out<<f.num<<"/"<<f.den;
    return out;
}
  
istream & operator >> (istream &in,  fraction &f)
{   cout << "Enter numerator Part ";
    in >> f.num;
    cout << "Enter denominator Part ";
    in >> f.den;
    return in;
}
class FracList{	//class fraclist
    fraction *fracPtr;
    int size;
public:
    FracList()	//default constructor
    {	fracPtr=NULL;
        size=0;
    }
    FracList(FracList &ptr)	//copy constructor
    {	fracPtr=ptr.fracPtr;
        size=ptr.size;
    }
    friend ostream & operator << (ostream &out, const FracList &c);
    friend istream & operator >> (istream &in,  FracList &c);
    
    int operator==(int i)	//overloading operator==
    {	if(*(fracPtr+i)==*(fracPtr))
    		return 1;
    	else
    		return 0;
    }
    int operator!=(int i)	//overloading operator!=
    {	if(*(fracPtr+i)!=*(fracPtr))
    		return 1;
    	else
    		return 0;
    }
 	int operator<(int i)	//overloading operator<
    {	if(*(fracPtr+i)<*(fracPtr))
    		return 1;
    	else
    		return 0;
    }
 	int operator>(int i)	//overloading operator>
    {	if(*(fracPtr+i)>*(fracPtr))
    		return 1;
    	else
    		return 0;
    }
    
    void sort()	//function to sort
    {	fraction temp;
        for(int i=0; i<size; i++)
            for(int j=i+1; j<size; j++)
                if(*(fracPtr+i)>*(fracPtr+j))
                {	temp=*(fracPtr+i);
                    *(fracPtr+i)=*(fracPtr+j);
                    *(fracPtr+j)=temp;
                }
    }
    
    fraction BinarySearch()
    {	fraction f;
        cout<<"\nEnter information you want to search : ";
        cin>>f;
        
        int start=0, end=size-1;
        int mid= (start+(end-start))/2;
        
        for(int i=start; i<=mid ;	mid= (start+(end-start))/2 )
        {	if(*(fracPtr+mid)==f)
                break;
            else if(*(fracPtr+mid)<f)
                        end=mid;
                else
                    start=mid+1;		
        }
        return f;
    }	
};

ostream & operator << (ostream &out, const FracList &f)
{	int i;
    for(i=0; i<f.size; i++)
        out<<f.fracPtr+i<<" ";
    return out;
}
  
istream & operator >> (istream &in, FracList &f)
{   cout << "Enter size ";
    in >> f.size;
    cout << "Enter values\n";
    for(int i=0; i<f.size; i++)
        //in >> f.fracPtr+i;
    return in;
}

int main()
{	fraction f;
    cin>>f;
    cout<<f;
    return 0;
}

Output

Fraction class

 

Also read, Answer the following Algebraic equations

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *