Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.

Similar presentations


Presentation on theme: "CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I."— Presentation transcript:

1 CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I

2 Recap of Lecture 10 Function Overloading Name Mangling Resolution of name and type Copy constructor

3 Outline of lecture 11 Operator overloading Unary operator Assignment operator Self assignment

4 Polymorphism Polymorphism in C++ Compile Time Function overloading Operator overloading Runtime Virtual Function

5 Operator overloading Compiler sees an expression consisting of an argument followed by an operator followed by an argument, it simply calls a function. An operator is simply a function call with a different syntax. There must be a previously declared function to match that operator Operator overloading is just “syntactic sugar”

6 Is the knowledge about operator wrong ? Maybe everything they know about operators in C is suddenly wrong Operators for built-in types won’t suddenly start working differently Overloaded operators can be created only where new data types are involved 1 << 4; 1.414 << 1; won’t suddenly change its meaning won’t suddenly start working.

7 Operator overloading Specify more than one definition for an operator in the same scope Declared (or defined) previously in the same scope The compiler determines the most appropriate definition to use Overload resolution Redefine or overload most of the built-in operators available

8 We are already overloading Inserters ( >) A stream is an object that formats and holds bytes. int i; cin >> i; float f; cin >> f; char c; cin >> c; char buf[100]; cin >> buf;

9 Operator overloading Overloaded operators are functions with special names The overloaded function will perform relative to the class upon which it will work The keyword operator followed by the symbol for the operator being defined Operator functions can be member or non-member of a class Non-member operator function : friend functions

10 Creating a Member operator function The general form of such a function is Ret-type classname::operator#(arg-list){ //operations } // Overload + for loc. loc loc::operator+(loc op2) Demonstration

11 Can we overload all operators +-*/%^ &|~!,= <><=>=++-- <<>>==!=&&|| +=-=/=%=^=&= |=*=<<=>>=[]() ->->*newnew []deletedelete [] ::.*.?:

12 Parameter passing to operator function Operator function Global Unary (1) Binary (2) Member Unary (0) Binary (1) The number of arguments in the overloaded operator’s argument list depends on two factors

13 Restrictions Cannot combine operators that currently have no meaning Cannot change the evaluation precedence of operators Cannot change the number of arguments required by an operator

14 Overloading Unary Operator Takes just one operand Takes no argument Example: Increment (++) and decrement(--) operator Unary minus (-) operator The logical not (!) operator The unary operators operate on the object for which they were called

15 Operator chaining Assignment is right-associative In order to support operator chaining, the assignment operator must return some value The value that should be returned is a reference to the left- hand side of the assignment. But should this reference be a const or nonconst? int a, b, c, d, e; a = b = c = d = e = 42;

16 Self assignment MUST CHECK FOR SELF-ASSIGNMENT !! Especially important when your class does its own memory allocation MyClass& MyClass::operator=(const MyClass &rhs) { // 1. Deallocate any memory that MyClass is using internally // 2. Allocate some memory to hold the contents of rhs // 3. Copy the values from rhs into this instance // 4. Return *this } MyClass mc;... mc = mc;

17 How to recover self assignment There are many ways to answer the question Are these two instances the same? Just compare the two objects' addresses MyClass& MyClass::operator=(const MyClass &rhs) { if (this != &rhs) {... // Deallocate, allocate new space, copy values... } return *this; }

18 Assignment operator The guidelines for the assignment operator are: Take a const-reference for the argument (the right-hand side of the assignment). Return a reference to the left-hand side, to support safe and reasonable operator chaining. (Do this by returning *this.) Check for self-assignment, by comparing the pointers (this to &rhs).

19 Thank you Next Lecture: Operator Overloading-II


Download ppt "CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I."

Similar presentations


Ads by Google