# Fractions

• 04-28-2003
Aakash Datt
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: ... }```
• 04-28-2003
orbitz
You might want to put them into the global namespace.
• 04-28-2003
Jamsan
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.
• 04-29-2003
Mecnels
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).
• 04-29-2003
Aakash Datt
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.
• 04-29-2003
Mecnels
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.
• 04-29-2003
Aakash Datt
...
[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.
• 04-29-2003
NebulousMenace
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
• 04-30-2003
Aakash Datt
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; }```