overloading operator problems

This is a discussion on overloading operator problems within the C++ Programming forums, part of the General Programming Boards category; this piece of code was fine when i replaced operator+-*/ with add, subtract, multiply, and divide. now i am getting ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    10

    overloading operator problems

    this piece of code was fine when i replaced operator+-*/ with add, subtract, multiply, and divide. now i am getting errors that there are too many parameters in the overloading operators. i am not too familar with overloading operators. should i create a new class that contains all the parameters? a friend function (which i am also not too familar with)? also, is anybody familar with using the assert() method in the <cassert> library so the program will print out an error if a user inputs "0" as the denominator?

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    ///////////////////////////// fraction Class ///////////////////////////////
    class fraction
    {
    
    private:
    	int num;
    	int denom;
    	char dummychar;
    
    public:
    
    
    	fraction() : num(0), denom(0)
    	{  }                                  // Constructor
    
    	void getfraction()                    // Input fractions and sign
    	{
    		cout << "Please Enter Fraction: ";
    	    cin >> num >> dummychar >> denom;
    	    cout << "\n";
    	}
    
    	
    	void operator+(fraction& fr1, fraction& fr2, fraction& fr3);  // decl. of addition function
    	void operator-(fraction& fr1, fraction& fr2, fraction& fr3);  // decl. of subtract.function
    	void operator*(fraction& fr1, fraction& fr2, fraction& fr3);  // decl. of multipl. function
    	void operator/(fraction& fr1, fraction& fr2, fraction& fr3);    // decl. of division function
    	void lowterms(fraction& fr3);               // declaration of lowterms function
    		
    	void showfraction(fraction& fr3)            // Display result on the screen
    	{
    		cout << "Answer= " << fr3.num << "/" 
    			 << fr3.denom << "\n\n";
    	}
    
    
    };
    
    
    
    // ------------------------------ Addition ------------------------------
    
    void fraction::operator+(fraction& fr1, fraction& fr2, fraction& fr3)
    {
    	fr3.num = fr1.num*fr2.denom + fr1.denom*fr2.num;
    	fr3.denom = fr1.denom*fr2.denom;
    }
    
    // ----------------------------- Subtraction-----------------------------
    
    void fraction::operator-(fraction& fr1, fraction& fr2, fraction& fr3)
    {
    	fr3.num = fr1.num*fr2.denom - fr1.denom*fr2.num;
    	fr3.denom = fr1.denom*fr2.denom;
    }
    
    // ---------------------------- Multiplication---------------------------
    
    void fraction::operator*(fraction& fr1, fraction& fr2, fraction& fr3)
    {
    	fr3.num = fr1.num*fr2.num;
    	fr3.denom = fr1.denom*fr2.denom;
    }
    
    // ------------------------------ Division ------------------------------
    
    void fraction::operator/(fraction& fr1, fraction& fr2, fraction& fr3)
    {
    	fr3.num = fr1.num*fr2.denom;
    	fr3.denom = fr1.denom*fr2.num;
    }
    
    // ------------------------ Lowest Term Function ------------------------
    
    void fraction::lowterms(fraction& fr3)                  // Simplify to lowest terms
    		{
    			long tnum, tden, temp, gcd;
    
    			tnum = labs(fr3.num);
    			tden = labs(fr3.denom);
    			
    			if(tden==0)
    			{ cout << "Illegal fraction: division by 0 \n"; exit(1); }
    			else if(tnum==0)
    			{ fr3.num=0; fr3.denom=1; return; }
    
    			while(tnum != 0)
    			{
    				if(tnum < tden)
    				{ temp=tnum; tnum=tden; tden=temp; }
    			tnum = tnum - tden;
    			}
    		gcd = tden;
    		fr3.num = fr3.num / gcd;
    		fr3.denom = fr3.denom / gcd;
    
    		}
    
    char getsign();                        // declaration of getsign function
    
    //////////////////////////////////////////////////////////////////////////
    
    int main()
    {
    	fraction fract1, fract2, fract3;
    	char response;
    	char sgn;
    
    
    
    	while (response != 'N')
    	{
    
    		fract1.getfraction();
    		fract2.getfraction();
    		sgn = getsign();
    		
    
    		switch (sgn)                   // Arithmetic Switch
    		{
    		case '+':
    			fract3.operator+(fract1, fract2, fract3);
    			break;
    		case '-':
    			fract3.operator-(fract1, fract2, fract3);               
    			break;
    		case '*':
    			fract3.operator*(fract1, fract2, fract3);             
    			break;
    		case '/':
    			fract3.operator/(fract1, fract2, fract3);                
    			break;
    		}
    
    		fract3.lowterms(fract3);                   // function call to simplify terms
    
    		fract3.showfraction(fract3);
    
    		cout << "Calculate Another (Y or N)? ";
    		cin >> response;
    		cout << "\n\n";
    
    	}
    
    	return 0;
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    
    char getsign()
    	{
            char sign;
    
    		cout << "Please Enter Mathematic Operation: ";
    		cin >> sign;
    		cout << "\n";
    		return sign;
    	}

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    operator+ should be defined like this (with one argument and returning an object):

    Code:
    const fraction fraction::operator+(const fraction& fr1
    {
    fraction fr;
    fr.num = fr1.num* this->denom+ fr.denom * this->num;
    fr.denom = this->denom * fr1.denom;
    return fr;
    }
    And likewise for the other operators
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you wanted to add a method to your class that allows the user to add a fraction to an int you could use a friend function (though there may other ways as well).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Operator= overloading from another type
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2006, 09:15 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 05:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21