Overloading Operators

I found this code online that overloads the multiplication operator using a fraction class. I have finals coming up and I'm trying to grasp the concept. Can anyone please explain to me how this code works and how I can make it use addition, subtraction and division?
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
#include <iostream>
 
class Fraction
{
    int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
    int n, d;
public:
    Fraction(int n, int d = 1) : n(n/gcd(n, d)), d(d/gcd(n, d)) { }
    int num() const { return n; }
    int den() const { return d; }
    Fraction& operator*=(const Fraction& rhs)
    {
        int new_n = n * rhs.n/gcd(n * rhs.n, d * rhs.d);
        d = d * rhs.d/gcd(n * rhs.n, d * rhs.d);
        n = new_n;
        return *this;
    }
};
std::ostream& operator<<(std::ostream& out, const Fraction& f)
{
   return out << f.num() << '/' << f.den() ;
}
bool operator==(const Fraction& lhs, const Fraction& rhs)
{
    return lhs.num() == rhs.num() && lhs.den() == rhs.den();
}
bool operator!=(const Fraction& lhs, const Fraction& rhs)
{
    return !(lhs == rhs);
}
Fraction operator*(Fraction lhs, const Fraction& rhs)
{
    return lhs *= rhs;
}
 
int main()
{
   Fraction f1(3, 8), f2(1, 2), f3(10, 2);
   std::cout << f1 << " * " << f2 << " = " << f1 * f2 << '\n'
             << f2 << " * " << f3 << " = " << f2 * f3 << '\n'
             <<  2 << " * " << f1 << " = " <<  2 * f1 << '\n';
}
I suggest that you use the debugger to find out the program flow. Especially the function gcd(...) (aka greates common divisior).

For % operator see:

http://www.cplusplus.com/doc/tutorial/operators/

For the algorithm to find the gcd see:

https://en.wikipedia.org/wiki/Greatest_common_divisor

how I can make it use addition, subtraction and division?
They are similar to multiplication. Once you understand that you can easily write the other.

Once again: I suggest that you use the debugger or debug [cout].
I think you know how the arithmetic of fractions is implemented. But, you can't grasp the idea of overloading operators right?

At its simplest:

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
#include <iostream>

class A
{
    public:

        int number = 0; // variable 'number' of type int

        A() { } // default constructor
        A(int n) : number(n) { } // constructor that takes in a number (int)
};

// std::cout has a type of std::ostream (also a class)
std::ostream& operator<<(std::ostream& out, A class_type) // overloaded function to print objects of this class
{
    return std::cout << class_type.number; // returns std::ostream after printing
}

A operator+(A class_type_1, A class_type_2) // overloaded function to add objects of this class
{
    // returns class type A that holds the added value of the two members number in its member number
    return A(class_type_1.number + class_type_2.number);
}

int main()
{
    A object1(5), object2(11);
    
    // the two arguments for the overloaded function (<<) is std::cout and object1 (type std::ostream and type A)
    std::cout << "Object 1: " << object1 << '\n' << "Object 2: " << object2 << '\n';
    
    // the two arguments for the overloaded function (+) is object1 and object2 (both of type A)
    std::cout << "Both objects added together: " << object1 + object2 << '\n';
}

Object 1: 5
Object 2: 11
Both objects added together: 16
Last edited on
Topic archived. No new replies allowed.