Overloading insertion and extraction problem

This is a discussion on Overloading insertion and extraction problem within the C++ Programming forums, part of the General Programming Boards category; Im having a difficult time wondering why im having problems with my insertion (<<) and extraction (>>) operators. If anyone ...

  1. #1
    Curwa
    Guest

    Unhappy Overloading insertion and extraction problem

    Im having a difficult time wondering why im having problems with my insertion (<<) and extraction (>>) operators. If anyone can help me out with this minor problem...it would be grateful. Thanks!

    Code:
    #include <math.h>         
    #include <iostream.h>       
    //using namespace std;
    
    class Fraction   
    {
    	private:
    		int denom;
    		int numer;
    		
    	public:
    		Fraction()
    			{
    				numer = 0;
    				denom = 1;
    			};
    		Fraction(int n, int d)
    			{
    				numer = n;
    				denom = d;
    			};
    		Fraction operator + (Fraction f2)
    			{
    				Fraction temp;
    				temp.numer =  numer * f2.denom + denom * f2.numer;
    				temp.denom =  denom * f2.denom;
    				return temp;
    			};
    		Fraction operator - (Fraction f2)
    			{
    				Fraction temp;
    				temp.numer =  numer * f2.denom - denom * f2.numer;
    				temp.denom =  denom * f2.denom;
    				return temp;
    			};
    		Fraction operator * (Fraction f2)
    			{				
    				Fraction temp;
    				temp.numer =  numer * f2.numer;
    				temp.denom =  denom * f2.denom;								
    				return temp;
    			};
    		Fraction operator / (Fraction f2)
    			{				 
    				Fraction temp;
    				temp.numer =  numer * f2.denom;
    				temp.denom =  denom * f2.numer;				
    				return temp;
    			};		
    		bool operator > (Fraction f1)
    			{
    				float temp = f1.numer / f1.denom;
    				float temp1 = numer / denom;				
                    return (temp > temp1) ? true:false;
    			};
    		bool operator < (Fraction f1)
    			{
    				float temp = numer / denom;
    				float temp1 = f1.numer / f1.denom;
    				return (temp < temp1) ? true:false;
    			};
    		bool operator == (Fraction f1)
    			{
    				if ((numer == f1.numer) && (denom == f1.denom))
    					return 1;
    				else
    					return 0;
    			}
    		bool operator != (Fraction f1)
    			{
    				if ((numer != f1.numer) && (denom != f1.denom))
    					return 1;
    				else
    					return 0;
    			}
    		void operator += (Fraction f1)
    			{
    				numer = numer + f1.numer;
    				denom = denom + f1.denom;
    			}
    		void operator -= (Fraction f1)
    			{
    				numer = numer - f1.numer;
    				denom = denom - f1.denom;
    			}
    		void operator *= (Fraction f1)
    			{
    				numer = numer * f1.numer;
    				denom = denom * f1.denom;
    			}
    		void operator /= (Fraction f1)
    			{
    				numer = numer / f1.numer;
    				denom = denom / f1.denom;
    			}
    		void lowTerms()
    			{
    				long rNumer, rDenom, temp, gcd;
    
    				rNumer = labs(numer);
    				rDenom = labs(denom);
    				if (rDenom == 0)
    					{
    						cout<<"Illegal Fraction: division by 0"<<endl;
    						return;
    					}
    				else if (rNumer == 0)
    					{
    						numer = 0;
    						denom = 1;
    						return;
    					}
    				while (rNumer != 0)
    					{
    						if (rNumer < rDenom)
    							{
    								temp = rNumer;
    								rNumer = rDenom;
    								rDenom = temp;						
    							}
    						rNumer -= rDenom;
    					}
    				gcd = rDenom;
    				numer = numer / gcd;
    				denom = denom / gcd;
    			}				
    
    
    };
    friend istream& operator >> (istream& in, Fraction f1)
    			{
    				char slash;
    				in>>numer;
    				cin>>slash;
    				in>>denom;
    				return;
    			}
    friend ostream& operator << (ostream& out, Fraction f1)
    			{
    				out<<numer;
    				cout<<"/";
    				out<<denom;
    				return out;
    			}
    
    void main()
    {
       Fraction f1, f2, f3, f4;      
       char   slash = '/';
       char   op;
       char   valid_operator= 'y';
       char   another_try='y';
    
       cout << "\n-------- The four-function calculator ---------" << endl;
       do {
           cout << "\nEnter a fraction, an operator (+, -, *, /), and another fraction: "
                   << "\n       (for example: 1/2 * 3/4)    ";         
    
    		  cin >> f1 >> op >> f2;
    		  switch (op)
                  {
                      case '+':
                          f3 = f1 + f2;                   	 
    					  f4 += f2;
    					  break;
    
                      case '-':
                          f3 = f1 - f2;
    					  f4 -= f2;
                          break;
    
                      case '*':
                          f3 = f1 * f2;
    					  f4 *= f2;
                          break;
    
                      case '/':
                          f3 = f1 / f2;
    					  f4 /= f2;
                          break;
                      default:
                          valid_operator = 'n';
                          break;
                  }
    
    	if (valid_operator == 'y')
            {
                cout << "       The result is "<<f3;
    			if ( f1 < f2 )
    				{							  	     
                        cout<<f1;
    					cout << " is less than ";
    					cout<<f2;
    					cout << endl;
    				}
    			if ( f1 > f2 )
    				{ 
    					cout<<f1; 
    					cout<<" is greater than ";
    					cout<<f2;
    					cout<<endl;
    				}
    			if ( f1 == f2 )
    				{ 
    					cout<<f1; 
    					cout << " is equal to ";
    					cout<<f2;
    					cout << endl;
    				}
    			if ( f1 != f2 )
    				{ 
    					cout<<f1; 
    					cout << " is not equal to ";
    					cout<<f2;
    					cout << endl;
    				}
    
            } 
    	if (valid_operator == 'y')
    		{
    			cout<< "       The result is ";
    			f3.lowTerms();
    			cout<<f3;
    			cout<< "\n       The assignment operator result is: ";
    			cout<<f4;
    	    }
    	else
    		{
    			cout << "       The operator must be +, -, *, or /. Try again! "  << endl;
    			valid_operator = 'y';
    		}
    	cout << "       Do another calculation?  ";
    	cin >> another_try;
    	} while (another_try == 'y');
    
    }

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You aren't understanding the prupose of friend functions -- the class has to be made known of which functions are friends from within the class's definition.

    Write "friend" followed by the declaration of the overloaded left sihft and right shift operators ( << and >> ) INSIDE of the class (otherwise you wouldn't be telling the compiler what class the function is a friend of), and the define the functions outside of the class as though they were just regular functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:17 AM
  2. overload operator+ in format (z + obj2)
    By slrj in forum C++ Programming
    Replies: 18
    Last Post: 10-11-2006, 03:55 PM
  3. overloading extraction and insertion
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 06:16 PM
  4. Overloading < or > operators
    By mouse163 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2003, 08:32 PM

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