Fractions

This is a discussion on Fractions within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing a Fraction class that overloads the operators +, -, *, /. But I'm not sure where to ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    31

    Fractions

    Hi,
    I'm writing a Fraction class that overloads the operators +, -, *, /. But I'm not sure where to the overloaded operators...

    Code:
    Here's my fraction.h
    
    class Fraction
    {
        friend Fraction operator +(Fraction first, Fraction second)
    
    Public:
    ...
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You might want to put them into the global namespace.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    class Fraction
    {
    	private:
    		int numerator;
    		int denominator;
    
    	public:
    		Fraction operator+ (Fraction B);
    		Fraction operator- (Fraction B);
    		Fraction operator* (Fraction B);
    		Fraction operator/ (Fraction B);
    };
    if you have say, temp = A + B, A is the one calling the +, so therefore you only need to send one argument to the function.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    60
    Operators like + or / should be friends of your fraction class.
    The easiest example where + as a member function fails is an expression like 7+somefraction. "7" can't be treated as a fraction object, though it is likely you want your operator+ to be able to calculate 7+somefraction. If operator+ is a global friend of your fraction-class, and your fraction-class has a constructor that can convert integers to fractions, 7+somefraction can be calculated.
    Also the operator==, operator< ... should be global friends of your fraction class, for it is likely you want to do something like
    7==fraction(21,3).

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    31

    Exclamation Errors...

    When I use the following header,

    Code:
    #ifndef _FRACTION_H
    #define _FRACTION_H
    
    	friend Fract operator+ (Fract first, Fract second);
    	friend Fract operator- (Fract first, Fract second);
    	friend Fract operator* (Fract first, Fract second);
    	friend Fract operator/ (Fract first, Fract second);
    
    class Fract
    {
    
    
    	public:
    		Fract (int numer, int denom);
    
    	private:
    		int numer;
    		int denom;
    };
    #endif
    the following errors occur:

    fraction.h(4) : error C2143: syntax error : missing ';' before '+'
    fraction.h(4) : error C2433: 'Fract' : 'friend' not permitted on data declarations
    fraction.h(4) : error C2501: 'Fract' : missing storage-class or type specifiers
    fraction.h(4) : error C2244: 'Fract' : unable to resolve function overload
    fraction.h(4) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    Inside your fraction-class

    You must declare friends inside the fraction class, think you are the compiler and get to this line

    friend fraction operator+(fraction first,fraction second);

    The compiler can't decide who's friend operator+ is. As soon as the compiler must guess you get errors.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    31

    ...

    [code]
    #ifndef _FRACTION_H
    #define _FRACTION_H

    #include <iostream>




    class Fraction
    {
    friend Fraction operator+ (Fraction first, Fraction second);
    friend Fraction operator- (Fraction first, Fraction second);
    friend Fraction operator* (Fraction first, Fraction second);
    friend Fraction operator/ (Fraction first, Fraction second);
    ostream & operator<< (ostream & output, const Fraction &f);

    public:
    Fraction (int numer, int denom);

    private:
    int numer;
    int denom;
    };
    #endif

    ---------------------------------------------------------------
    #include <iostream>
    #include "fraction.h"
    using namespace std;



    Fraction:: Fraction( int x, int y)
    :numer(x), denom(y)
    {}

    Fraction operator+ (Fraction first, Fraction second)
    {

    if(first.denom !=second.denom)
    {
    first.numer=first.numer*second.numer;
    second.numer=second.numer*first.denom;
    first.denom=second.numer*second.denom;
    }
    int sumNumer = first.numer+second.numer;
    int newDenom = first.denom;
    Fraction Result(sumNumer, newDenom);

    return Result;
    }

    ostream & operator<<(ostream & output, const Fraction &f);
    {
    output<< f.numer<<"/"<< f.denom<< " "<< endl;
    return output;
    }
    -----------------------------------------------------------------------
    #include "fraction.h"
    #include <iostream>
    using namespace std;


    int main()
    {
    Fraction first (1, 2);
    Fraction second (1, 4);

    cout<< first+second<< endl;

    return 0;
    }
    -----------------------------------------------------------------------

    returns:

    --------------------Configuration: fract - Win32 Debug--------------------
    Compiling...
    fraction.cpp
    fraction.h(15) : error C2143: syntax error : missing ';' before '&'
    fraction.h(15) : error C2501: 'ostream' : missing storage-class or type specifiers
    fraction.h(15) : error C2061: syntax error : identifier 'ostream'
    fraction.h(15) : error C2501: '<<' : missing storage-class or type specifiers
    fraction.h(15) : error C2805: binary 'operator <<' has too few parameters
    fraction.cpp(28) : error C2447: missing function header (old-style formal list?)
    client.cpp
    fraction.h(15) : error C2143: syntax error : missing ';' before '&'
    fraction.h(15) : error C2501: 'ostream' : missing storage-class or type specifiers
    fraction.h(15) : error C2061: syntax error : identifier 'ostream'
    fraction.h(15) : error C2501: '<<' : missing storage-class or type specifiers
    fraction.h(15) : error C2805: binary 'operator <<' has too few parameters
    client.cpp(11) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Fraction' (or there is no acceptable conversion)
    Error executing cl.exe.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    I'm not sure if I should start a new thread, but I'm looking for help, and I'm working on essentially the same program as the first guy. I've got all the math operators done and working. My problems are thus:

    1. How do I reduce my results? I've got a non-overload function that works inside a single class, but when I'm doing [class] a+b,
    I can't even figure out how to reference my results, much less reduce them.

    2. What do I do for == ? It's essentially the same issue - I know it's a boolean function, but I don't get what to refer to.

    Not sure what code would be helpful, so I attatched what I've done so far.

    Thanks for any help
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    31

    Unhappy Trouble

    O.K. When I try to separate my program into header, imple, and client, it gives me 100 errors. And also, if I use #include <iostream>, it gives me errors.
    Can anyone separate this for me?

    Code:
    #include "iostream.h"
    
    class fraction {
    private:
      int num;
      int den;
    
    public:
      fraction();
      fraction(int, int);
    
      fraction operator+(fraction);
      fraction operator+(int);
      fraction operator-(fraction);
      fraction operator*(fraction);
      fraction operator/(fraction);
    
      friend ostream&  operator<<(ostream &, fraction);
    
    };
    
    void reduce(int &, int &);
    
    fraction::fraction() : num (0), den (1) {}
    
    
    fraction::fraction(int n, int d) : num (n), den (d) { reduce( num, den);}
    
    fraction fraction::operator+(fraction b) {
      return fraction( (num*b.den)+(b.num*den) , (den*b.den) );
    }
    
    fraction fraction::operator+(int b) {
      return fraction( num +(b*den) , den );
    }
    
    fraction fraction::operator-(fraction b) {
      return fraction( (num*b.den)-(b.num*den) , (den*b.den) );
    }
    
    fraction fraction::operator*(fraction b) {
      return fraction( (num*b.num) , (den*b.den) );
    }
    
    fraction fraction::operator/(fraction b) {
      return fraction( (num*b.den) , (den*b.num) );
    }
    
    ostream& operator<<(ostream& os, fraction x) {
      return os << x.num << '/' << x.den;
    }
    
    int gcd(int a, int b) {
      int t;
      
      if (a < 0) a = -a;
      if (b < 0) b = -b;
    
      while (b) { t = b; b = a % t; a = t; }
    
      return a ;
    }
    
    void reduce(int &num, int &den) {
      int g = gcd(num, den);
    
      num /= g;  den /= g;
      if (den < 0) { num = -num; den = -den; }
    }
        
    int main() {
      fraction a(2,4), b(2,4), c(2,4), r;
      
      r = a + b + c;
      
      cout << a << " + " << b << " + " << c << " = " << r << '\n';;
    
      r = a + 3;
    
      cout << a << " + " << 3 << " = " << r << "\n" ;
    
      return 0;
    }
    Last edited by Aakash Datt; 04-30-2003 at 07:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 04:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 03:33 AM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 06:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

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