Saturday, December 17, 2011

How to Setup a Fraction Class Using Operator Overloading


3. How to Setup a Fraction Class Using Operator Overloading
This program shows how to setup and create a class which demonstrates how to overload operators including the insertion, extraction, > and + operators.
1. Click here to download the fraction class source files.  To begin, first set up the Driver, here a file called fraction_driver.cpp. :
//driver
#include <iostream>
#include "fraction.h" 
using namespace std; 
int main()
{
    fraction f1, f2; //f1 and f2 are objects: instance of the fraction                      //class
    fraction f3;     //f3 stores the answer of f1*f2
    cout <<"Enter the first fraction:  "<<endl;
    f1.readFrac();
    cout <<"Enter the second fraction:  "<<endl;
    f2.readFrac();
    f1.displayFrac();
    cout<<"*";
    f2.displayFrac();
    cout<<"=";
    f3=f1.multiply(f2);
    f3.displayFrac();
    cout <<endl;
    f1.displayFrac();
    cout<<"+";
    f2.displayFrac();
    cout<<"=";
    f3=f1.add(f2);
    f3.displayFrac();
    cout<<endl;
    //******************
    //overloads the + operator    
    f1.displayFrac();
    cout<<"+";
    f2.displayFrac();
    cout<<"=";
    //over loads the + operator
    f3=f1+f2; //overload the + operator to allow us to add
    f3.displayFrac();    
    cout <<endl;
    //***********************
    //overload the << (insertion operator)
    //display f3:
    cout<<f3<<endl;
    //****************************
    //overloads the >> extraction operator
    cout<<"Enter a fraction:  ";
    cin>>f1;
    cout<<f1<<endl;
    //************************
    //over loads the > operator
    cout<<"Enter a fraction:  ";
    cin>>f2;
    cout<<f2<<endl;
    if(f1 > f2)
       cout<<f1 <<" is greater than " << f2<<endl;
    else
       cout <<f1 <<" is not greater than "<<f2 <<endl;
    //************************    
    system("PAUSE");
    return 0;
}
2.  Next set up the header file, here named fraction.h
//header file
#include <iostream> //needs to be here if you are overloading << or >>
using namespace std;
#ifndef FRACTION_H //stops multiple declarations in memory of the
                   //fraction class
#define FRACTION_H //compiler directives
class fraction{
      public:
             fraction();  //default constructor...no incoming parameter
             fraction(int n, int d);
             void readFrac();
             void displayFrac();
             fraction multiply(fraction); //come in with a fraction, and
             //the result is a fraction
             //in multiply the fraction out front f1.multiply(f2) is a
             //is a given so don't say anything about it
             fraction add(fraction);
             fraction operator+(fraction &);  //prototype allow f1+f2  
             friend ostream& operator<<(ostream &, fraction &);
             //ostream is not a part of the fraction class, it is in the
             //iostream class             
             //so we say since we will use this class it is a friend
             //out file stream & reference object (fraction output)
             //function parameters: (ostream object (allows output),
             //fraction is your fraction
             friend istream& operator>>(istream &, fraction &);
             //main changes istream means infile stream,
             //>> (extraction) operator (istream & and , fraction &))
             int operator >(fraction &);
             //int is return type no need mentioning a class, > operator
             //you are coming in with a fraction (objects usually always
             //passed by reference         
     private:
              int num;
              int denom;              
      };
      #endif //compiler directive
3.  Finally setup the implementation file, here named faction.cpp
//implementation
#include <iostream>
#include "fraction.h"
using namespace std;
      fraction::fraction()  //default constructor...no incoming parameter
      {//set variables equal to 0
          num=0;
          denom=0;
      }
      fraction::fraction(int n, int d)  //constructor
      {//set variables equal to 0
          num=n;
          denom=d;
      }
      void fraction::readFrac()
      {
           char slash;
           cout<<"Enter a fraction (i.e. 3/4):  ";
           cin>>num>>slash>>denom;
       }
       void fraction::displayFrac()
      {
            cout<<num<<"/"<<denom;
      }
      fraction fraction::multiply(fraction f)
      {
           fraction temp(num*f.num, denom*f.denom);
           return temp;
      }
      fraction fraction::add(fraction f)
      {
           fraction temp(num*f.denom+denom*f.num, denom*f.denom);
           return temp;
      }
      //shows how to overload an operator
      fraction fraction::operator+(fraction &f)
      {
           fraction temp(num*f.denom+denom*f.num, denom*f.denom);
           return temp;              
      }
      ostream & operator<<(ostream &o, fraction &f) //can think of o as
      //cout<<
      {  //ostream is the return type by reference
         //o is exactly like cout <<f.num<<"/"<<f.denom;
              o<<f.num<<"/"<<f.denom;
              return o;             
      }
      istream & operator>>(istream &i, fraction &f)
      {   
           char slash;
           //i is exactly like cin>>f.num>>slash>>f.denom;
           i>>f.num>>slash>>f.denom;
           return i; 
      }
       int fraction::operator >(fraction &f)
       {
           int result;
           double firstF, secondF;
           firstF=((double)(num))/((double)(denom));
           secondF=((double)(f.num))/((double)(f.denom));
           if(firstF>secondF)
             //return 1;
             result=1;
           else
             //return 0;
             result= 0;
           return result;
           //return num*f.denom>denom*f.num; //another way to do all the  
           //lines of code above
       }

0 comments:

Post a Comment