DOC PREVIEW
IUPUI CS 265 - Operator Overloading

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Operator OverloadingOperator OverloadingRequirements of Overloaded OperatorsForms of Overloaded OperatorsOperator FunctionsOperator Functions (cont)Restrictions of Overloaded OperatorsUnary Overloaded Operators -- Member FunctionsBinary Overloaded Operators -- Member FunctionsOperator Overloading Using a Friend FunctionOperator Functions as Class Members vs. as friend FunctionsGlobal Operator OverloadingOverloading of Operators Having a Variable ArityOperators with Prefix and Postfix FormsPrefix Overloaded Function -- ExamplePostfix Overloaded Function -- ExampleSpecial Overloading FormsOverloading Stream-Insertion and Stream-Extraction OperatorsSlide 19Slide 20Slide 21Converting between TypesConverting between Types (cont)Special overloading forms - ExampleAcknowledgements1Dale RobertsOperator OverloadingOperator OverloadingDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] of Computer and Information Science,School of Science, IUPUIDale Roberts2Operator Overloading Operator Overloading FFunction-call notation is cumbersome for certain kinds of classes, especially unction-call notation is cumbersome for certain kinds of classes, especially mathematical classesmathematical classes Allows extendable design Allows extendable design Most appropriate for math classes. eg. Matrix, Vector, etc. Most appropriate for math classes. eg. Matrix, Vector, etc. Gives Operators Class-Specific Functionality Gives Operators Class-Specific Functionality In-built or Standard Overloading for Basic Numerical Data Types -- In-built or Standard Overloading for Basic Numerical Data Types -- ++ can be used with can be used with int, float, doublesint, float, doubles Analogous to Function Overloading -- Analogous to Function Overloading -- operator@operator@ is Used as the Function Name is Used as the Function Name 40 Operators can be Overloaded to Give Class-Specific Functionality 40 Operators can be Overloaded to Give Class-Specific Functionality C++ enables programmers to overload operators to be sensitive to the context in C++ enables programmers to overload operators to be sensitive to the context in which they are used. The compiler generates appropriate code which they are used. The compiler generates appropriate code Easier to read Easier to readDale Roberts3Requirements of Overloaded Operators Requirements of Overloaded Operators Their Meaning Should be Intuitive -- Their Meaning Should be Intuitive -- ++ Should Mean Should Mean Addition Addition When Appropriate, they Should be Associative -- When Appropriate, they Should be Associative -- a + ba + b Should Result in an Object, Should Result in an Object, cc of the Same Class of the Same Class If these Conditions are Not Satisfied then it is Better to If these Conditions are Not Satisfied then it is Better to Use Member Functions and Not Operator Overloading Use Member Functions and Not Operator Overloading To use an operator on class objects, that operator must To use an operator on class objects, that operator must be overloaded - with two exceptions - the assignment be overloaded - with two exceptions - the assignment operator (=), which performs a member wise copy, and operator (=), which performs a member wise copy, and the address (&) operator the address (&) operatorDale Roberts4Forms of Overloaded Operators Forms of Overloaded Operators Member Functions Member Functions Friend Functions Friend Functions Free-Standing or Global Functions Free-Standing or Global FunctionsDale Roberts5Operator Functions Operator Functions When to make class members, friends or global When to make class members, friends or global functions? functions? If member function, then If member function, then thisthis is implicitly available for is implicitly available for one of the arguments one of the arguments When overloading ( ), [ ], ->, or =, the operator When overloading ( ), [ ], ->, or =, the operator overloading function must be declared as a class overloading function must be declared as a class member. For other operators, the overloading functions member. For other operators, the overloading functions can be non-members can be non-members When an operator function is implemented as a member When an operator function is implemented as a member function, the left most (or only in the case of unary function, the left most (or only in the case of unary operators) operand must be a class object (or a operators) operand must be a class object (or a reference to a class object) of operator's class reference to a class object) of operator's class If the left operand must be an object of a different class If the left operand must be an object of a different class or a built-in type, this operator must be implemented as or a built-in type, this operator must be implemented as a non-class member. eg. <<, >> operatorsa non-class member. eg. <<, >> operatorsDale Roberts6Operator Functions (cont)Operator Functions (cont)An operator function implemented as a non-member An operator function implemented as a non-member must be a friend if it needs to access non-public data must be a friend if it needs to access non-public data members of that class. members of that class. The overloaded The overloaded <<<< operator must have a left operand of operator must have a left operand of type type ostreamostream. Therefore, it must be a non-member . Therefore, it must be a non-member function. Also, it may require access to the private data function. Also, it may require access to the private data members of the class. Thus, it needs to be a friend members of the class. Thus, it needs to be a friend function for that class. function for that class. Similar observation holds for Similar observation holds for >>>> operator which has a operator which has a left operand of type left operand of type istreamistream. . Operator member functions are classed only when the Operator member functions are classed only when the left operand of a binary operator is specifically an object left operand of a binary operator is specifically an object of that class or when the single operand of a unary of that class or when the single operand of a unary operator is an object of that class. operator is an object of that class. If the operator needs to be commutative (a + b = b + a), If the operator needs to be commutative (a + b = b + a), then making it a non-member function is necessary. then making it a non-member function is


View Full Document

IUPUI CS 265 - Operator Overloading

Download Operator Overloading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Operator Overloading and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Operator Overloading 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?