Tobias Sundell
Tobias Sundell

Reputation: 117

C++ Operator Overload Confusion

So I've created this class which can add the two values of two objects. What I don't really understand is in the operator function, does the values of numerator and denominator come from the object no1 in int main()?

class frac
{
    public:
        frac operator+(frac&);
        frac();
        frac(int, int);
        int numerator;
        int denominator;
};

frac frac::operator+(frac& tmp)
{
    frac tmpResult;
    tmpResult.numerator = numerator + tmp.numerator;
    tmpResult.denominator = denominator + tmp.denominator;

    return tmpResult;
}

int main()
{
    frac no1(2, 5);
    frac no2(3, 6);
    frac result = no1 + no2;
    return 0;
}

Upvotes: 0

Views: 180

Answers (6)

diegoperini
diegoperini

Reputation: 1806

Yes, you are right. Class members can be accessed within class methods without specifying their origin (as if they are declared locally).

frac frac::operator+(frac& tmp)
{
    frac tmpResult;
    tmpResult.numerator = numerator + tmp.numerator;
    tmpResult.denominator = denominator + tmp.denominator;

    return tmpResult;
}

This code can be rewritten as this:

frac frac::operator+(frac& tmp)
{
    frac tmpResult;
    tmpResult.numerator = this->numerator + tmp.numerator;
    tmpResult.denominator = this->denominator + tmp.denominator;

    return tmpResult;
}

PS: I believe you meant operator overloading.

Edit: I had this urge to correct your math as you are dealing with fractions wrong.

Fraction addition (and substraction respectively) can be done like this:

frac frac::operator+(frac& tmp)
{
    int common_denominator = tmp.denominator * this->denominator;
    int common_numerator = tmp.numerator * this->denominator + this->numerator * tmp.denominator;

    //You can add simplification by dividing these 
    //two above with their greatest common divisor 
    //in order to avoid cases like 111/222 which can
    //be represented as 1/2. (this is optional)

    return frac(common_numerator, common_denominator)
}

Upvotes: 0

Zac Howland
Zac Howland

Reputation: 15872

Ignoring your mathematical errors ...

When you declare an overloaded operator as a member function, the first parameter is assumed to be this. That is, no1 + no2 is just shorthand for no1.operator+(no2). Thus, the function sees numerator and denominator from the current no1 object (since it is on the left).

You could, alternatively, declare the function this way:

class frac
{
    // class declaration
    friend frac operator+(const frac& lhs, const frac& rhs);
};

frac operator+(const frac& lhs, const frac& rhs)
{
    // implement fraction addition for lhs + rhs
}

Upvotes: 0

Slava
Slava

Reputation: 44238

Your main() could be rewritten to be more explicit:

int main()
{
    frac no1(2, 5);
    frac no2(3, 6);
    frac result = no1.operator+( no2 );
    return 0;
}

This code is equivalent, but could be easier to understand which object pointer passed as this into operator+

Upvotes: 0

Shumail
Shumail

Reputation: 3143

YES. Values of numerator and denominator come from Object no 1.

For your understanding, you can also write it as:

frac result = no1.operator+( no2 );

Upvotes: 0

Daniel Frey
Daniel Frey

Reputation: 56863

Yes, it is defined as a member function, so it is equivalent to call

no1.operator+(no2);

You want to change the parameter type to a const reference, as you don't really want to modify the right hand side. And you want to fix your math.

Note you could also use a free function

frac operator+(const frac& lhs, const frac& rhs) { ... }

which seems more natural to some people (including me).

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 224864

does the values of numerator and denominator come from the object no1 in int main()?

Yes.

Upvotes: 1

Related Questions