Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Object Oriented Analysis and Design Polymorphism (Using C++)

Similar presentations


Presentation on theme: "CS212: Object Oriented Analysis and Design Polymorphism (Using C++)"— Presentation transcript:

1 CS212: Object Oriented Analysis and Design Polymorphism (Using C++)

2 Recap Function overloading Constructor overloading Copy constructor Object Oriented Analysis and Design (CS 212)

3 What happens if we don’t use it? Compiler creates one for you in the case of simple structures The default primitive behaviour: a bit-copy. C++ compiler will still automatically create a copy-constructor if you don’t make one The process the compiler goes through to synthesize a copy- constructor is called member-wise initialization.

4 Issues with pointers 50x8975 5 A B {B=A;} Initial object Duplicate object

5 Deep vs Shallow copy Copy constructor is called only for initializations For assignment ? Operator overload ??

6 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,” which means it is simply another way for you to make a function call.

7 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.

8 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

9 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;

10 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

11 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

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

13 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

14 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

15 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

16 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;

17 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;

18 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; }

19 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).


Download ppt "CS212: Object Oriented Analysis and Design Polymorphism (Using C++)"

Similar presentations


Ads by Google