CSC241 Object-Oriented Programming (OOP) Lecture No. 9.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Introduction to Programming Lecture 31. Operator Overloading.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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+
1 CSC241: Object Oriented Programming Lecture No 22.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
OPERATOR OVERLOADING Understand Operator Overloading and how it is implemented in C++ ? | Website for students | VTU NOTES.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
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.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
Object-Oriented Programming (OOP) Lecture No. 16
Yan Shi CS/SE 2630 Lecture Notes
Object-Oriented Programming (OOP) Lecture No. 15
Object-Oriented Programming (OOP) Lecture No. 21
Object-Oriented Programming (OOP) Lecture No. 17
Object-Oriented Programming (OOP) Lecture No. 16
Jim Fawcett Copyright ©
LinkedList Class.
The dirty secrets of objects
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object-Oriented Programming (OOP) Lecture No. 36
Object-Oriented Programming (OOP) Lecture No. 18
Group Status Project Status.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object-Oriented Programming (OOP) Lecture No. 37
Object-Oriented Programming (OOP) Lecture No. 20
Recap Week 2 and 3.
Constructors and Destructors
CISC/CMPE320 - Prof. McLeod
Object-Oriented Programming (OOP) Lecture No. 33
Introduction to Programming
Java Programming Language
Programming in C Pointers and Arrays.
Object oriented programming (OOP) Lecture No. 6
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Jim Fawcett Copyright ©
Copy Constructors and Overloaded Assignment
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 9

Binary operators ► Overloading + operator: class Complex{ private: double real, img; public: … Complex operator +(const Complex & rhs); };

Binary operators Complex Complex::operator +(const Complex & rhs){ Complex t; t.real = real + rhs.real; t.img = img + rhs.img; return t; }

Binary operators ► The return type is Complex so as to facilitate complex statements like: Complex t = c1 + c2 + c3; The above statement is automatically converted by the compiler into appropriate function calls: (c1.operator +(c2)).operator +(c3);

Binary operators ► The binary operator is always called with reference to the left hand argument ► Example:  In c1+c2, c1.operator+(c2)  In c2+c1, c2.operator+(c1)

Binary operators ► The above examples don’t handle the following situation: Complex c1; c To do this, we have to modify the Complex class

Binary operators ► Modifying the complex class: class Complex{... Complex operator +(const Complex & rhs); Complex operator +(const double& rhs); };

Binary operators Complex operator + (const double& rhs){ Complex t; t.real = real + rhs; t.img = img; return t; }

Binary operators ► Now suppose: Complex c2, c3; We can do the following: Complex c1 = c2 + c3; and Complex c4 = c ;

Binary operators ► But problem arises if we do the following: Complex c5 = c1; ► The + operator is called with reference to ► No predefined overloaded + operator is there that takes Complex as an argument

Binary operators ► Now if we write the following two functions to the class, we can add a Complex to a real or vice versa: class Complex{ … friend Complex operator + (const Complex & lhs, const double & rhs); friend Complex operator + (const double & lhs, const Complex & rhs); };

Binary operators Complex operator +(const Complex & lhs, const double& rhs) { Complex t; t.real = lhs.real + rhs; t.img = lhs.img; return t; }

Binary operators Complex operator + (const double & lhs, const Complex & rhs) { Complex t; t.real = lhs + rhs.real; t.img = rhs.img; return t; }

Binary operators class Complex{ … Complex operator + (const Complex &); friend Complex operator + (const Complex &, const double &); friend Complex operator + (const double &, const Complex &); };

Binary operators ► Other binary operators are overloaded very similar to the + operator as demonstrated in the above examples ► Example: Complex operator * (const Complex & c1, const Complex & c2); Complex operator / (const Complex & c1, const Complex & c2); Complex operator - (const Complex & c1, const Complex & c2);

Assignment operator ► Consider a string class: class String{ int size; char * bufferPtr; public: String(); String(char *); String(const String &); … };

Assignment operator String::String(char * ptr){ if (ptr != NULL){ size = strlen(ptr); bufferPtr = new char[size + 1]; strcpy(bufferPtr, ptr); } else{ bufferPtr = NULL; size = 0; } }

Assignment operator String::String(const String & rhs){ size = rhs.size; if (rhs.size != 0){ bufferPtr = new char[size + 1]; strcpy(bufferPtr, ptr); } else bufferPtr = NULL; }

Assignment operator int main(){ String str1(“Hello”); String str2(“World”); str1 = str2; return 0; } Member wise copy assignment

Assignment operator ► Result of str1 = str2 (memory leak) str1 Hello str2 World

Assignment operator ► Modifying: class String{ … public: … void operator =(const String &); };

Assignment operator void String::operator = (const String & rhs){ size = rhs.size; delete[] bufferPtr; if (rhs.size != 0){ bufferPtr = new char[rhs.size + 1]; strcpy(bufferPtr, rhs.bufferPtr); } else bufferPtr = NULL; }

Assignment operator int main(){ String str1(“ABC”); String str2(“DE”), str3(“FG”); str1 = str2; // Valid… str1 = str2 = str3; // Error… return 0; }

Assignment operator ► str1=str2=str3 is resolved as: str1.operator=(str2.operator= (str3)) Return type is void. Parameter can’t be void

Assignment operator ► Solution: modify the operator = function as follows: class String{ … public: … String & operator = (const String &); };

Assignment operator String & String :: operator = (const String & rhs){ size = rhs.size; delete[] bufferPtr; if (rhs.size != 0){ bufferPtr = new char[rhs.size + 1]; strcpy(bufferPtr, rhs.bufferPtr); } else bufferPtr = NULL; return *this; }

Assignment operator void main(){ String str1("AB"); String str2("CD"), str3("EF"); str1 = str2; str1 = str2 = str3; // Now valid… }

Assignment operator ► str1=str2=str3 is resolved as: str1.operator=(str2.operator= (str3)) Return type is String Reference.

Assignment operator int main(){ String str1("Fakhir"); // Self Assignment problem… str1 = str1; return 0; }

Assignment operator String & String :: operator = (const String & rhs){ size = rhs.size; delete[] bufferPtr; if (rhs.size != 0){ bufferPtr = new char[rhs.size + 1]; strcpy(bufferPtr, rhs.bufferPtr); } else bufferPtr = NULL; return *this; }

… // size = rhs.size; // delete [] bufferPtr; … ??? Assignment operator ► Result of str1 = str1 str1 Fakhir

Assignment operator String & String :: operator = (const String & rhs){ if (this != &rhs){ size = rhs.size; delete[] bufferPtr; if (rhs.bufferPtr != NULL){ bufferPtr = new char[rhs.size + 1]; strcpy(bufferPtr, rhs.bufferPtr); } else bufferPtr = NULL; } return *this; }

Assignment operator ► Now self-assignment is properly handled: int main(){ String str1("Fakhir"); str1 = str1; return 0; }

Assignment operator ► Solution: modify the operator= function as follows: class String{ … public: … const String & operator =(const String &); };

Assignment operator int main(){ String s1("ABC"), s2("DEF"), s3("GHI"); // Error… (s1 = s2) = s3; return 0; }

Assignment operator But we can do the following with primitive types: int main(){ int a, b, c; (a = b) = c; return 0; }

Other Binary operators ► Overloading += operator: class Complex{ double real, img; public: Complex & operator+=(const Complex & rhs); Complex & operator+=(const double & rhs);... };

Other Binary operators Complex & Complex::operator += (const Complex & rhs){ real = real + rhs.real; img = img + rhs.img; return *this; }

Other Binary operators Complex & Complex::operator +=(const double & rhs){ real = real + rhs; return *this; }

Other Binary operators int main(){ Complex c1, c2, c3; c1 += c2; c3 += 0.087; return 0; }

Operator overloading ► Friend functions minimize encapsulation ► This can result in:  Data vulnerability  Programming bugs  Tough debugging ► Hence, use of friend functions must be limited

Operator overloading ► The + operator can be defined as a non-member, non-friend function: Complex operator + (const Complex & a, const Complex & b){ Complex t = a; return t += b }

Operator overloading Complex operator + (const double & a, const Complex & b){ Complex t = b; return t += a; }

Operator overloading Complex operator + (const Complex & a, const double & b){ Complex t = a; return t += b; }

Other Binary operators The operators -=, /=, *=, |=, %=, &=, ^=, >=, != can be overloaded in a very similar fashion