Operater Overloading Using 'class' Keyword (OOP)-4

Posted on at


PROBLEM:

Modify the fraction class in the four-function fraction calculator from Exercise 11 inChapter 6 so that it uses overloaded operators for addition, subtraction, multiplication,and division. (Remember the rules for fraction arithmetic in Exercise 12 in Chapter 3,“Loops and Decisions.”) Also overload the == and != comparison operators, and use themto exit from the loop if the user enters 0/1, 0/1 for the values of the two input fractions.You may want to modify the lowterms() function so that it returns the value of its argumentreduced to lowest terms. This makes it more useful in the arithmetic functions, where itcan be applied just before the answer is returned.   

PROGRAM:

#include "stdafx.h"

#include "iostream"

#include "math.h"

using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////

class fraction

{

            private:

                        intnum, den;                char dummy;

            public:

                        fraction ( ): num(0), den(1)

                                    {/*Empty Body*/}

                        fraction (int n, int d): num(n), den(d)

                                    {/*Empty Body*/}

                        voidget_F ( )

                        {           cout<< "Enter Fraction (in the form of a/b): ";   cin>>num>> dummy >> den;         }

                        bool operator == (fraction f2) const;

                        bool operator != (fraction f2) const;

                        voidshow_F (fraction f1, fraction f2, char op) const

                        {           cout<< f1.num << "/" << f1.den << op;          cout<< f2.num << "/" << f2.den << " = ";

                                    cout<<num<< "/" << den <<endl;                   }

                        fraction operator + (fraction);             fraction operator - (fraction);

fraction operator * (fraction);              fraction operator / (fraction);

fractionlowterms( );

};

//---------------------------------------------------------------------------------------

bool fraction::operator == (fraction f2) const

{

            if(num == 0 && den == 1 && f2.num == 0 && f2.den == 1)              return true;

            else                  return false;

}

bool fraction::operator != (fraction f2) const

{

            if(num != 0 && den != 1 && f2.num != 0 && f2.den != 1)                   return true;

            else                  return false;

}

fraction fraction::operator + (fraction f2)

{           intn,d;              n = (num*f2.den + den*f2.num);        d = den*f2.den;           return fraction(n,d);     }

fraction fraction::operator - (fraction f2)

{           intn,d;              n = (num*f2.den - den*f2.num);         d = den*f2.den;           return fraction(n,d);     }

fraction fraction::operator * (fraction f2)

{           intn,d;  n = num * f2.num;       d = den * f2.den;         return fraction(n,d);                 }

fraction fraction::operator / (fraction f2)

{           intn,d;  n = num * f2.den;        d = den * f2.num;        return fraction(n,d);                 }

fraction fraction::lowterms ( )

{

            longtnum, tden, temp, gcd;

            tnum = labs(num);

            tden = labs(den);

            if(tden==0 )

                        {           cout<< "Illegal fraction: division by 0";  exit(1);          }

            else if( tnum==0 )

            {           num=0; den = 1; return fraction(num,den);    }

            while(tnum != 0)

            {

                        if(tnum<tden)

                                    {           temp=tnum;                tnum=tden;                  tden=temp;      }

                        tnum = tnum - tden;

            }

            gcd = tden;

            num = num / gcd;

            den = den / gcd;

            return fraction(num,den);

}

/////////////////////////////////////////////////////////////////////////////////////////void main( )

{

            fraction frac1, frac2, frac3,frac4;

            cout<< "Enter '0/1' & '0/1' for both fractions to exit.\n";

            frac1.get_F( );             frac2.get_F( );

            if(frac1 == frac2)

            {           cout<< "GOOD BYE!\n";        exit(1);}

            if(frac1 != frac2)

            {

                        frac3 = frac1 + frac2;frac4 = frac3.lowterms( );       frac4.show_F(frac1,frac2,'+');

                        frac3 = frac1 - frac2;  frac4 = frac3.lowterms( );       frac4.show_F(frac1,frac2,'-');

                        frac3 = frac1 * frac2;  frac4 = frac3.lowterms( );       frac4.show_F(frac1,frac2,'*');

                        frac3 = frac1 / frac2;  frac4 = frac3.lowterms( );       frac4.show_F(frac1,frac2,'/');

            }

            system("pause");

}

OUTPUT:

                         

PROBLEM:

Augment the safearay class in the ARROVER3 program in this chapter so that the usercan specify both the upper and lower bound of the array (indexes running from 100 to200, for example). Have the overloaded subscript operator check the index each time thearray is accessed to ensure that it is not out of bounds. You’ll need to add a twoargumentconstructor that specifies the upper and lower bounds. Since we have not yetlearned how to allocate memory dynamically, the member data will still be an array thatstarts at 0 and runs up to 99, but perhaps you can map the indexes for the safearay intodifferent indexes in the real int array. For example, if the client selects a range from 100to 175, you could map this into the range from arr[0] to arr[75].

Program:

 

#include "stdafx.h"

 

#include "iostream"

 

using namespace std;

 

constint LIMIT = 100;

 

/////////////////////////////////////////////////////////////////////////////////////////

 

classsafearay

 

{

 

            private:

 

                        intarr[LIMIT], u_limit, l_limit;

 

            public:

 

                        safearay(int l, int u) : l_limit(l), u_limit(u)         { }

 

                        int& operator [] (int n)              //note: return by reference

 

                        {

 

                                    l_limit -= (l_limit - 0);               u_limit -= (u_limit - 100);

 

                                    if( n <l_limit || n >= u_limit )

 

                                                {           cout<< "\nIndex out of bounds\n";                  exit(1);            }

 

                                    returnarr[n];

 

                        }

 

};

 

/////////////////////////////////////////////////////////////////////////////////////////

 

void main()

 

{

 

            int low, up, j;

 

            cout<< "Enter Lower & Upper Limits of an Array of 100 Elements Respectively: ";

 

                        cin>> low >> up;

 

            safearay sa1(low,up);

 

            for(j = low; j < up; j++)

 

                        sa1[j-low] = (j-low)*10;

 

            for(j = low; j < up; j++)

 

                        {           int temp = sa1[j-low];               cout<< "Element " << j << " is " << temp <<endl;      }

 

            system("pause");

}

OUTPUT:

                 

 



About the author

MuhammadTayyab

Me from Pakistan and im the student of undergraduate;BS Electronics.

Subscribe 0
160