CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Operator Overloading Fundamentals
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Classes Separating interface from implementation
Classes: A Deeper Look Systems Programming.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Operator Overloading and Type Conversions
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
Operator Overloading; String and Array Objects
CS 2304: Operator Overloading
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
9-10 Classes: A Deeper Look.
Operator Overloading; String and Array Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
Class: Special Topics 2 For classes using memory allocation
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II

Recap of Lecture 11 Operator overloading Unary operator Assignment operator Self assignment

Outline of lecture 12 Self assignment Use of Friend Function Type Conversion

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? (a=b)=c; int a, b, c, d, e; a = b = c = d = e = 42;

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;

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

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

Using a Friend Function Overload an operator for a class by using a non-member function Usually a friend of the class Overloaded friend operator function is passed the operands explicitly Binary operator: two parameters, unary operator: one parameter. Demonstration

Flexibility with Friend Function Overload a binary operator by using a member function Object on the left side of the operator generates the call Further, a pointer to that object is passed in the this pointer. Ob // valid Ob // invalid Demonstration

New() and Delete() These operators can be overloaded globally or for a specific classes. Usually programmers do not overload them globally. void* operator new (size_t n) { //allocate memory and return the address } void operator delete (void *p) { //free the memory space pointer to by the pointer ‘p’ }

New() and delete() for Arrays // Allocate an array of objects. void *operator new[]( size_t size) { /* Perform allocation. Throw bad_alloc on failure. Constructor for each element called automatically. */ return pointer_to_memory; } // Delete an array of objects. void operator delete[] (void *p) { /* Free memory pointed to by p. Destructor for each element called automatically. */ }

Operator comma It is called when it appears next to an object of the type the comma is defined for. “operator,” is not called for function argument lists Only for objects that are out in the open, separated by commas. Demonstration

Some observations Return by value as const The return optimization return Integer(left.i + right.i); Integer tmp(left.i + right.i); return tmp; “make a temporary Integer object and return it.” Three things will happen.

Basic guidelines OperatorRecommended use All unary operatorsMember = ( ) [ ] –> and –>*must be member += –= /= *= ^= &= |= %= >>= <<= Member All other binary operators Non-member

Automatic type conversion If the compiler sees an expression or function call A type that isn’t quite the one it needs It can often perform an automatic type conversion From the type it has (source) to the type it wants (target) For user-defined types: automatic type conversion functions A particular type of constructor and an overloaded operator.

Constructor conversion A constructor that takes as its single argument an object (or reference) The type of object is different i.e. of another type Allows the compiler to perform an automatic type conversion Preventing constructor conversion: explicit

Operator conversion The second way is through operator overloading A member function that takes the current type and converts it to the desired type This form of operator overloading is unique You don’t appear to specify a return type The return type is the name of the operator you’re overloading. Demonstration

Pitfalls of type conversion Compiler must choose how to quietly perform a type conversion It can get into trouble if conversions are not designed correctly Ambiguity error when that conversion occurs Demonstration

Thank you Next Lecture: Inheritance