{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect10_1 - Operator Functions as Nonmember Functions 1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Operator Functions as Nonmember Functions 1 Operator Overloading Operator Functions as Nonmember Functions We have seen earlier that C++ supports function overloading ; so we can use the same name for two or more functions in the same program. C++ language supports operator overloading as well. By overloading an operator, we can define our own meaning to the operator whenever we use that operator together with an object of our own defined type. Recall that in the program discussed in Part 2, we used a non-member function addRationalNums as follows. RationalNum a(3, 7), b(1,4), c; c = addRationalNums(a, b); c.outputRationalNum(); // outputs 19 / 28 We would like to write the above code as follows. RationalNum a(3, 7), b(1,4), c; c = a + b; cout << c ; // must output 19 / 28 For a programmer, the above code is more direct, expressive and convenient. Even though, objects a , b , and c are not basic types , we want to use them as if they are basic types. If the variables a and b are basic types such as an int or double , the compiler understands the meaning of the + operator as addition. If the objects are user- defined types such as RationalNum , statements such as a + b and cout << c are meaningless for the compiler. We can specify a meaning for the operators + and << relative to our type RationalNum by overloading these operators. The word operator is a keyword in C++. We can use operator+ as a function name. In general, we can use operator followed by a specific C++ operator symbol as a function name. Examples are, operator+ , operator- , operator++ , operator-- , operator= , operator+= , operator< , operator== , operator<< , etc.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Operator Functions as Nonmember Functions 2 Overloading + Consider the following function definition, which is a rewritten version of our earlier addRationalNums function. RationalNum operator+(RationanNum lhs, RationalNum rhs) { int numerator = (lhs.getNumer() * rhs.getDenom()) + (rhs.getNumer() * lhs.getDenom()); int denominator = lhs.getDenom() * rhs.getDenom(); RationalNum r(numerator,denominator); r.simplify(); return r; } // end operator+ function The definition of the overloaded operator + is basically the same as the definition of the function addRationalNums . The only difference is that we replace the func- tion name with operator+ . Notice that the above function, as was the case with addRationalNums , is a non-member function. Therefore, we can call this function directly as follows. RationalNum a(3, 7), b(1,4), c; c = operator+(a, b); // a call to operator+ The function call operator+(a,b) will invoke the above function. The first function parameter lhs will be set equal to that of object a , and the second parameter rhs with object b . The function returns a RationalNum object which is the sum of a and b . Indeed, we can also call the operator+ function like this. RationalNum a(3, 7), b(1,4), c; c = a + b; // same as operator+(a,b); The compiler automatically replaces the statement a + b with operator+(a,b) .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}