Thread: Operator overloading

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    52

    Operator overloading

    I am having a difficult time figuring out how to overload the addition '+' operator to add two fractions from my Fraction Class.
    I've searched this site and pored over several books only to find nothing pertaining to my delimma.

    Obviously, this is a school assignment and this is for class credit. I would like to further UNDERSTAND the concept of operator overloading and am not asking for someone to write this for me, however, I would greatly appreciate ANY and ALL suggestions concerning this.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    #include <stdlib.h>
    
    class Fraction
    {
    private:
    	int WholeNumber;
    	int Numerator;
    	int Denominator;
    
    public:
    	Fraction()
    	{
    		WholeNumber = 0;
    		Numerator = 0;
    		Denominator = 1;
    	}
    
    	Fraction(int w, int n, int d):
    	WholeNumber(w), Numerator(n), Denominator(d)
    	{}
    
    	Fraction(const Fraction & rhs)
    	{
    		WholeNumber = rhs.getWholeNumber();
    		Numerator = rhs.getNumerator();
    		Denominator = rhs.getDenominator();
    	}
    
    	int getWholeNumber(void) const { return WholeNumber; } //provide definition here...
    	int getNumerator(void) const { return Numerator; }
    	int getDenominator(void) const { return Denominator; }
    
    	void getFraction(void) const
    	{
    		cout << WholeNumber;
    		if(Numerator)
    			cout << " " << Numerator
    			<< "/"
    			<< Denominator;
    	}
    
    	void setFraction(int w, int n, int d)
    	{
    	  int fract = (w * n + d)/ d; //WholeNumber * Denominator + Numerator
    	}
    
    	void setFraction(void)
    	{
    		char Temp[] = "                ";
    
    		char *pChr;
    
    		char w[8], n[8], d[8];
    
    		for(int count = 0; count < 7; count++)
    		{
    			w[count] = ' ';
    			n[count] = ' ';
    			d[count] = ' ';
    		}
    		
    		d[count] = '\0';
    		n[count] = '\0';
    		w[count] = '\0';
    
    		pChr = &Temp[15];
    		cin.getline(Temp,15);
    
    		while(*pChr == ' ')
    		{
    			pChr--;
    		}
    		*pChr = '\0';
    		
    		int NullLocation = (pChr - Temp);
    
    		pChr--;
    
    		char *pDen = &d[6];
    		char *pNum = &n[6];
    		char *pWhole = &w[6];
    
    		for(count = 0; count < NullLocation; count++)
    		{
    			if(Temp[count] == '/')
    			break;
    		}
    
    		if(count >= NullLocation)
    		{
    			Denominator = 1;
    			Numerator = 0;
    		}
    		else
    		{
    			while(*pChr != '/')
    			{
    				*pDen = *pChr;
    				pDen--;
    				pChr--;
    			}
    			Denominator = atoi(d);
    
    			pChr--;
    			while(*pChr != ' ')
    			{
    				*pNum = *pChr;
    				pNum--;
    				pChr--;
    			}
    			Numerator = atoi(n);
    		}//End else
    
    		while(pChr >= &Temp[0])
    		{
    			*pWhole = *pChr;
    			pWhole--;
    			pChr--;
    		}
    
    		WholeNumber = atoi(w);
    	}
    	
    	void AddFractions(Fraction, Fraction);
    	void ReduceFractions(void);
    
    	Fraction Fraction::operator + (Fraction & rhs);
    
    		
    
    };
    
    
    
    	void Fraction::AddFractions(Fraction One, Fraction Two)
    	{
    		int New1;
    		int New2;
    		int NewNumerator, 
    			CarryOut;
    
    		WholeNumber = One.WholeNumber + Two.WholeNumber;
    		Denominator = One.Denominator + Two.Denominator;
    		New1 = One.Numerator * Two.Denominator;
    		New2 = Two.Numerator * One.Denominator;
    		NewNumerator = New1 + New2;
    		CarryOut = NewNumerator / Denominator;
    		WholeNumber += CarryOut;
    		Numerator = NewNumerator % Denominator;
    	}
    
    	void Fraction::ReduceFractions(void)
    	{
    		int First = Numerator;
    		int Second = Denominator;
    		int Temp;
    
    		if(Numerator ==0)
    		{
    			Denominator = 1;
    			return;
    		}
    
    		while(First != Second)
    		{
    			if(First < Second)
    			{ 
    				Temp = First;
    				First = Second;
    				Second = Temp;
    			}
    			First -= Second;
    		}
    
    		Numerator /= First;
    		Denominator /= First;
    	}
    
    	
    	int main(void)
    	{
    		Fraction FirstOne;
    		Fraction SecondOne;
    		Fraction Sum;
    
    		cout << "Enter the first fraction value: " << endl;
    		FirstOne.setFraction();
    
    		cout << "Enter the second fraction value: " << endl;
    		SecondOne.getFraction();
    
    		Sum.AddFractions(FirstOne, SecondOne);
    
    		Sum.ReduceFractions();
    
    		cout << endl << "The sum of ";
    		FirstOne.getFraction();
    		cout << " and ";
    		SecondOne.getFraction();
    		cout << " is ";
    		Sum.getFraction();
    		cout << endl << endl;
    
    		Fraction NewSum = FirstOne + SecondOne;
    
    		cout << endl << "The sum of ";
    		FirstOne.getFraction();
    		cout << " and ";
    		SecondOne.getFraction();
    		cout << " is ";
    		Sum.getFraction();
    		cout << endl << endl;
    
    		return 0;
    	}
    
    	
    	Fraction::operator +(Fraction &fract)
    	{
    		Fraction temp;
    
    		temp = Fraction(WholeNumber * Denominator + Numerator) + (fract.getWholeNumber() * fract.getDenominator() + fract.getNumerator();
    		return Fraction::setFraction(temp);
    	}
    I've got everything else in place (or so I believe). This is ticking me off, considering that the next thing I have to do is overload many more operators. Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    Real quick...


    Before I am upbraided for posting an operator question yet again...

    I have searched the site, reviewed many of the posts and read books. So, either I'm the most ignorant person and should be flipping burgers (which isn't too improbable) or I am having a difficult time understanding and then implementing it into my program.

    Defensive??? I'm going on 2 hours of sleep and lots of Ethiopian coffee...

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Here you go. I just did this assignment (or something similar, my class is using Deitel's book), so it's not going to be exactly like yours, but you should be able to figure it out.

    Euclid.h
    Code:
    #ifndef Euclid_H  
    #define Euclid_H 
    
    class Euclid
    {
    public:
    	friend ostream &operator<<(ostream &, const Euclid &);
    	Euclid(int = 1,int = 1);		//default constructor
    	Euclid &operator=(Euclid &); //^  
    	Euclid operator+(Euclid &);   //^
    	Euclid &operator+=(Euclid &); //^
    	Euclid operator-(Euclid &);    //^
    	Euclid &operator-=(Euclid &);  //^
    	Euclid operator*(Euclid &);   //^
    	Euclid &operator*=(Euclid &); //^
    	Euclid operator/(Euclid &);   //^
    	Euclid &operator/=(Euclid &); //^
    	bool operator<(Euclid &);       //^
    	bool operator<=(Euclid &);  //^
    	bool operator>(Euclid &);    //^
    	bool operator>=(Euclid &);  //^
    	bool operator!=(Euclid &);    //^
    	bool operator==(Euclid &);   //^
    private:
    	int numerator;
    	int denominator;
    	int simplify(int, int);
    };
    #endif
    (The little //^ things are a note to myself that I have completed that function. Just ignore them.)

    The + operator
    Code:
    Euclid Euclid::operator +(Euclid &fract)
    {
    	Euclid temp(1,1);
    	
    	temp.numerator  = ((numerator * fract.denominator) + (denominator * fract.numerator));
    	
    	temp.denominator = (denominator * fract.denominator);
    
    	int gcd = simplify(temp.numerator, temp.denominator);
           //^^^simplify returns the greatest common denominator
    
    	temp.numerator = (temp.numerator)/gcd;
    	temp.denominator = (temp.denominator)/gcd;
    
    	return temp;
    }
    I created a temporary class so that neither of the fractions being added would be changed. Then I just did the math, simplified the fraction, and returned the temp object.

Popular pages Recent additions subscribe to a feed