DOC PREVIEW
IUPUI CS 265 - Operator Overloading

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 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 24 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 24 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 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Operator OverloadingOperator OverloadingRequirements of Overloaded OperatorsForms of Overloaded OperatorsOperator FunctionsRestrictions 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 18Slide 19Slide 20Converting between TypesConverting between Types (cont)Special overloading forms - ExampleAcknowledgementsDale RobertsOperator OverloadingOperator OverloadingDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] of Computer and Information Science,School of Science, IUPUIDale RobertsOperator 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 read Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[]Dale RobertsRequirements 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 RobertsForms 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 RobertsOperator Functions Operator Functions When to make class members, friends or global functions? When to make class members, friends or global functions? If member function, then If member function, then thisthis is implicitly available for one of the arguments is implicitly available for one of the arguments When overloading ( ), [ ], ->, or =, the operator overloading function must be declared When overloading ( ), [ ], ->, or =, the operator overloading function must be declared as a class member. For other operators, the overloading functions can be non-as a class member. For other operators, the overloading functions can be non-members members When an operator function is implemented as a member function, the left most (or When an operator function is implemented as a member function, the left most (or only in the case of unary operators) operand must be a class object (or a reference to only in the case of unary operators) operand must be a class object (or a reference to a class object) of operator's class a class object) of operator's class If the left operand must be an object of a different class or a built-in type, this If the left operand must be an object of a different class or a built-in type, this operator must be implemented as a non-class member. eg. <<, >> operatorsoperator must be implemented as a non-class member. eg. <<, >> operatorsAn operator function implemented as a non-member must be a friend if it needs to An operator function implemented as a non-member must be a friend if it needs to access non-public data members of that class. access non-public data members of that class. The overloaded The overloaded <<<< operator must have a left operand of type operator must have a left operand of type ostreamostream. Therefore, it . Therefore, it must be a non-member function. Also, it may require access to the private data must be a non-member function. Also, it may require access to the private data members of the class. Thus, it needs to be a friend function for that class. members of the class. Thus, it needs to be a friend function for that class. Similar observation holds for Similar observation holds for >>>> operator which has a left operand of type operator which has a left operand of type istreamistream. . Operator member functions are classed only when the left operand of a binary Operator member functions are classed only when the left operand of a binary operator is specifically an object of that class or when the single operand of a unary operator is specifically an object 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), then making it a non-member If the operator needs to be


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?