1 / 14

Operator Overloading Member Functions

Department of Computer and Information Science, School of Science, IUPUI. Operator Overloading Member Functions. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Operator Overloading.

Download Presentation

Operator Overloading Member Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI Operator OverloadingMember Functions Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Operator Overloading • Function-call notation is cumbersome for certain kinds of classes, especially mathematical classes • Allows extendable design • Most appropriate for math classes. eg. Matrix, Vector, etc. • Gives Operators Class-Specific Functionality • In-built or Standard Overloading for Basic Numerical Data Types -- + can be used with int, float, doubles • Analogous to Function Overloading -- operator@ is Used as the Function Name • 40 Operators can be Overloaded to Give Class-Specific Functionality

  3. Operator Overloading (Cont) C++ enables programmers to overload operators to be sensitive to the context in which they are used.  The compiler generates appropriate code Easier to read

  4. Requirements of Overloaded Operators • Their Meaning Should be Intuitive -- + Should Mean Addition • When Appropriate, they Should be Associative -- a + b Should Result in an Object, c of the Same Class • If these Conditions are Not Satisfied then it is Better to Use Member Functions and Not Operator Overloading • To use an operator on class objects, that operator must be overloaded - with two exceptions - the assignment operator (=), which performs a member wise copy, and the address (&) operator

  5. Forms of Overloaded Operators • Member Functions • Friend Functions • Free-Standing or Global Functions

  6. Operator Functions • When to make class members, friends or global functions? • If member function, then this is implicitly available for one of the arguments • When overloading ( ), [ ], ->, or =, the operator overloading function must be declared as a class member.  For other operators, the overloading functions can be non-members

  7. Operator Functions (Cont) • 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 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 operator must be implemented as a non-class member. eg. <<, >> operators

  8. Operator Functions (cont) • An operator function implemented as a non-member must be a friend if it needs to access non-public data members of that class. • The overloaded << operator must have a left operand of type ostream.  Therefore, it 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.

  9. Operator Functions (Cont) • Similar observation holds for >> operator which has a left operand of type istream. • 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 an object of that class. • If the operator needs to be commutative (a + b = b + a), then making it a non-member function is necessary.

  10. Restrictions of Overloaded Operators • New Operators CANNOT be Created • Fundamental Data Types (e.g. int) CANNOT be Overloaded • Operator Priority CANNOT be Changed • Operator Associativity CANNOT be Changed • The arity of CANNOT be changed -- + can Take ONLY TWO Arguments (there is no unary +) • Two Separate Overloaded Functions (With Different Signatures) can be Created for Operators Which Exist in Pre-fix and Post-fix Form -- ++

  11. Restrictions of Overloaded Operators (Cont) • Overloaded Operators are NOT IMPLICITLY Associative or Commutative, Even if the Original Operators were Associative or Commutative -- Associativity and Commutativity must be explicitly implemented. For Associativity this means returning an instance of the class. • Overloading the operator + does not automatically overload related operators (+=, ++, etc).  If needed, these related operators must be explicitly overloaded

  12. Unary Overloaded Operators -- Member Functions • Invocation in Two Ways -- Object@ (Direct) or Object.operator@() (As a Function) class number{ int n; public: number(int x = 0):n(x){}; number operator-(){return number (-n);} }; main() { number a(1), b(2), c, d; //Invocation of "-" Operator -- direct d = -b; //d.n = -2 //Invocation of "-" Operator -- Function c = a.operator-(); //c.n = -1 }

  13. Binary Overloaded Operators -- Member Functions • Invocation in Two Ways -- ObjectA @ ObjectB (direct) or ObjectA.operator@(ObjectB) (As a Function) class number{ int n; public: number(int x = 0):n(x){}; number operator+(number ip) {return number (ip.n + n);} }; main() { number a(1), b(2), c, d; //Invocation of "+" Operator -- direct d = a + b; //d.n = 3 //Invocation of "+" Operator -- Function c = d.operator+(b); //c.n = d.n + b.n = 5 }

  14. Acknowledgements • These slides were originally development by Dr. Uday Murthy and Dr. Rajeev Raje. • Some contents comes from the Deitel slides that accompany your text. • Some information regarding the postfix form the increment and decrement operators comes from MSDN.

More Related