operator overloading addition

solved
Last edited on
Well it is clear that you are not defining the same operator that you are declaring.

1
2
3
4
5
6
7
8
9
10
Rational operator + (const Rational &obj) const; // overloaded the addition sign 

Rational Rational::operator + (const Rational obj) const // not the same, missing ampersand (&)
{
	Rational temp;
	temp.numerator = numerator * obj.denominator + denominator*obj.numerator;
	temp.denominator = denominator * obj.denominator;
	return temp;

}


Apart from that, I hope you do have a default constructor Rational::Rational() which initializes numerator and denominator to sane values. Garbage values are sometimes a sign of forgotten initialization.

Finally... it is good practice to overload operator+ and family externally to the class.

This is because by having them as members you force the first parameter to be an object of your class. This matters when you want to add other things than Rational to your object.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Rational
{
    // ...

public:

    Rational operator + (int i) const
    {
        // ..
    }
};

int main()
{
    Rational r;

    Rational result1 = r + 1; // works
    Rational result2 = 1 + r; // doesn't work
}


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
class Rational
{
    // ...

public:

    friend Rational operator + (const Rational &, int);
    friend Rational operator + (int, const Rational &);
};

Rational operator + (const Rational &r, int i)
{
    // ...
}


Rational operator + (int i, const Rational &r)
{
    // ...
}

int main()
{
    Rational r;

    Rational result1 = r + 1; // works
    Rational result2 = 1 + r; // works
}

Last edited on
yes i did do them in the class header file and i do have the default constructor, but for the sake of ease i did not post the entire code because the problem is with the operator overloading not anything else.

and when i tried that code instead of it giving me what i want, it gives me a weird number -858993460 for addition of the 2 fractions for example when i did 3/8 + 1/6 it gave me -858993460/-858993460
Last edited on
Why are you now returning Rational(numerator,denominator) instead of temp?

In fact I see the problem now:

16
17
18
19
20
    Rational(Rational& obj)
	{
	 int numerator = obj.numerator;
	 int denominator = obj.denominator;
	}


This copy constructor creates two local variables named numerator and denominator, but doesn't change the actual data member with the same name.

16
17
18
19
20
21
22
23
24
25
    Rational(const Rational& obj)
	{
	 numerator = obj.numerator;
	 denominator = obj.denominator;

	 // or...

	 // this->numerator = obj.numerator;
	 // this->denominator = obj.denominator;
	}


Or using C++ initialization lists:

16
17
18
19
20
    Rational(const Rational& obj):
		numerator(obj.numerator),
		denominator(obj.denominator)
	{
	}
First implement the compound assignment operators operator+=, operator*= etc. as member functions.
Test and verify that they are working correctly.

Then it is canonical to implement operator+, operator* etc. in terms of the compound assignment operators.

Note: Do not implement foundation operations like the copy constructor, operator= etc.; let them be implicit.

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

struct rational
{
    explicit rational( int n = 0, int d = 1 ) : num(n), den(d) {}

    rational& operator+= ( const rational& other )
    {
        num = num * other.den + other.num * den ;
        den *= other.den ;
        return *this ;
    }

    rational& operator+= ( int v )
    {
        num = num + v * den ;
        return *this ;
    }

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

    private:
        int num ;
        int den ;
};

// operator+ is implemented in terms of += 

// note: a is passed by value
rational operator+ ( rational a, const rational& b ) { return a += b ; }

// note: r is passed by value
rational operator+ ( rational r, int i ) { return r += i ; }
rational operator+ ( int i, rational r ) { return r += i ; }

std::ostream& operator<< ( std::ostream& stm, const rational& r )
{ return stm << '(' << r.numerator() << '/' << r.denomiator() << ')' ; }

int main()
{
    std::cout << rational{3,2} + rational{1,3} << '\n'
              << rational{3,5} + 2 << '\n'
              << 7 + rational{5,9} << '\n' ;
}

http://coliru.stacked-crooked.com/a/2085dab25dcbd2d8
Topic archived. No new replies allowed.