OOP TEST 2

OOP TEST 2 - OOP TEST 2 Operator Overloading +Operator...

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

View Full Document Right Arrow Icon
OOP TEST 2 Operator Overloading +Operator overloading contributes to C++’s extensibility (capability of being enhanced with new features). +Use operator overloading when it makes a program clearer than accomplishing the same operators with function calls. +Overloaded operators should mimic the functionality of their built-in counterparts – for example, the + operator should be overloaded to perform addition, not subtraction. Avoid excessive or inconsistent use of operator overloading, as this can make a program cryptic and difficult to read. +When operators are overloaded as member functions, they must be non- static, because they must be called on an object of the class and operate on that object. +To use an operator on class objects, that operator must be overloaded, with three exceptions: the assignment operator (=) may be used with every class to perform member-wise assignment of the data members of the class – each data member is assigned from the ‘source’ object to the ‘target’ object of the assignment. Such default member-wise assignment is dangerous for classes with pointer members; we should implicitly overload the assignment operators for such classes. The address operator (&); that returns the address of the object in memory, and the comma (,) operator, that evaluates the expression to its left, then the expression to its right, may also be used w/objects of any class without overloading. +Overloading is specially appropriate for mathematical classes.
Background image of page 1

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

View Full DocumentRight Arrow Icon
+The precedence of an operator(*,/ over +, - ) cannot be overloaded, however parenthesis can be used to force the order of evaluation of overloaded operators in an expression. +Operators that cannot be overloaded: “::”, “.”, “.*”, “?:(C++’s only ternary operator)”. +The associativity of an operator (whether the operator is applied right-to- left or left-to-right) cannot be changed by overloading. +It is not possible to change the “arity” or “aryness” (number of operands an operator takes). Operators &, *, +, -   all have both unary and binary versions, these unary and binary versions can be overloaded. +It is not possible to create new operators;only existing operators can be overloaded. This prevents programmers from using popular notations like the ** operator (C++’s ^ operator, which can be overloaded) used in other programming languages. +Operator overloading works only with objects of user-defined types or with
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.

This note was uploaded on 04/30/2008 for the course COMP 345 taught by Professor Ragsdale during the Fall '07 term at Harding.

Page1 / 8

OOP TEST 2 - OOP TEST 2 Operator Overloading +Operator...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online