Problem with operator overloading

I'm a little confused about using operator overloading with fractions. I want to have a member function that allows the display of an object's data vallues. I also need to have one that allows 2 fractions to be multiplied. Can anyone help me out with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include <iostream>
#include <cmath>

using namespace std;

int a, b, c;

class Fractions
{
    private:
 int num;
 int denom;
 public:
 Fractions(int=1, int=1);
 void operator!(void) const;
 Fractions operator+(int fractions&) const;
};

Fractions::Fractions(int n, int d)
{
    if( d != 0)
    num= n;
    denom= d;
}

Fractions Fractions:: operator+(int num, int denom, int num[1], int denom[1])
{
    a= num/denom;
    b= num[1]/denom[1];
    c= a + b;
    c= (num * denom[1]+ denom * num[1])/(denom * denom[1]);
    return c;
}

int main()
{  
   return 0;
}
Last edited on
Line 16 should be:

Fractions operator+(const Fractions&) const;


Line 26 should be:

Fractions Fractions::operator+(const Fractions& f) const
Now the compiler is saying that I have invalid data types on my subscripted items. what should I do?
Heres my new code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>

using namespace std;

int a, b, c;

class Fractions
{
    private:
 int num;
 int denom;
 public:
 Fractions(int=1, int=1);
 void operator!(void) const;
 Fractions operator+(const Fractions&) const;
};

Fractions::Fractions(int n, int d)
{
    if( d != 0)
    num= n;
    denom= d;
}

Fractions Fractions::operator+(const Fractions& f) const
{
    a= num/denom;
    b= num[1]/denom[1];
    c= a + b;
    c= (num * denom[1]+ denom * num[1])/(denom * denom[1]);
    return c;
}

int main()
{  
   return 0;
}
Last edited on
Now the compiler is saying that I have invalid data types on my subscripted items. what should I do?


If you have int num; describe what you believe num[1] is. We can see that operator+ has a parameter f. Don't you think you should use it somehow?
I'm supposed to use it to add two fractions so num[1] is the second numerator. Also, I'm a bit confused as to what the f parameter is actually there for.
Last edited on
I'm supposed to use it to add two fractions so num[1] is the second numerator.

If num was an array, num[1] would be the second element in the array, but as your compiler rightly points out, num is not an array.



Also, I'm a bit confused as to what the f parameter is actually there for.
f is the other fraction. The source of the second numerator/denominator.


Perhaps this example will be illuminating.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

class Fraction
{
public:
    Fraction() : num(1), den(1) {}
    Fraction(int numerator, int denominator) 
        : num(numerator), den(denominator) {}

    Fraction& operator+=(Fraction f);

    int numerator() const { return num; }
    int denominator() const { return den; }

private:
    int num;
    int den;
};

Fraction& Fraction::operator+=(Fraction f)
{
    if (den != f.den)
    {
        int new_base = den * f.den;

        num *= new_base / den;
        den = new_base;

        f.num *= new_base / f.den;
        f.den = new_base;
    }

    num += f.num;
    return *this;
}

// operator+ is commonly implemented in terms of operator+=
Fraction operator+(Fraction a, const Fraction& b)
{
    return a += b;
}

std::ostream& operator<<(std::ostream& os, const Fraction& f)
{
    os << f.numerator();

    if (f.denominator() != 1)
        os << '/' << f.denominator();

    return os;
}

int main()
{
    Fraction a(1, 5);
    Fraction b(2, 5);

    std::cout << a << " + " << b << " = " << a + b << '\n';
}
Last edited on
Okay that is very helpful. Can I make an array to allow the user to change the values of the fractions?
Topic archived. No new replies allowed.