Object-Oriented Programming (OOP) Lecture No. 20

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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Introduction to Programming Lecture 31. Operator Overloading.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Software Design and C++ Programming Lecture 4 Operator Overloading and Streamed I/O.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Lecture 4 OOP Course. 4. Operators Using constructors: String int main(){ String s1(“My String”); String s2(s1); String s3; s3=s1; } int main(){ String.
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Operator overloading Object Oriented Programming.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
1 CSC241: Object Oriented Programming Lecture No 22.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Object-Oriented Programming (OOP) Lecture No. 16
Andy Wang Object Oriented Programming in C++ COP 3330
CS410 – Software Engineering Lecture #12: C++ Basics V
Operator Overloading Ritika Sharma.
Object-Oriented Programming (OOP) Lecture No. 15
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9: Operator Overloading
CSC241: Object Oriented Programming
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.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Operators Lecture 10 Fri, Feb 8, 2008.
Object-Oriented Programming (OOP) Lecture No. 36
Object-Oriented Programming (OOP) Lecture No. 39
Object-Oriented Programming (OOP) Lecture No. 18
Pointers, Dynamic Data, and Reference Types
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 14
Chapter 15 Pointers, Dynamic Data, and Reference Types
Operator Overloading, Friends, and References
CISC/CMPE320 - Prof. McLeod
Object-Oriented Programming (OOP) Lecture No. 33
COP 3330 Object-oriented Programming in C++
Introduction to Programming
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Object-Oriented Programming (OOP) Lecture No. 34
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Miscellaneous Topics I
Introduction to Programming
Jim Fawcett Copyright ©
CS410 – Software Engineering Lecture #6: Advanced C++ I
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 20

Other Binary operators We have seen the following string class till now: class String{ private: char * bufferPtr; int size; public: String(); String(char * ptr); void SetString(char * ptr); const char * GetString(); ... };

Other Binary Operators int main(){ String str1(“Test”); String str2; str2.SetString(“Ping”); return 0; }

Other Binary Operators What if we want to change the string from “Ping” to “Pong”?? {ONLY 1 character to be changed…} Possible solution: Call: str2.SetString(“Pong”); This will delete the current buffer and allocate a new one Too much overhead if string is too big

Other Binary Operators Or, we can add a function which changes a character at nth location class String{ ... public: void SetChar(char c, int pos); };

Other Binary Operators void SetChar(char c, int pos){ if(bufferPtr != NULL){ if(pos>0 && pos<=size) bufferPtr[pos] = c; }

Other Binary Operators Now we can efficiently change a single character: String str1(“Ping”); str1.SetChar(‘o’, 2); // str1 is now changed to “Pong”

An elegant solution: Overloading the subscript “[]” operator

Subscript Operator int main(){ String str2; str2.SetString(“Ping”); str[2] = ‘o’; cout << str[2]; return 0; }

class String{ ... public: char & operator[](int); }; Subscript Operator class String{ ... public: char & operator[](int); };

Subscript Operator char & String::operator[]( int pos){ assert(pos>0 && pos<=size); return stringPtr[pos-1]; }

Subscript Operator int main() { String s1(“Ping”); cout <<str.GetString()<< endl; s1[2] = ‘o’; cout << str.GetString(); return 0; }

Subscript Operator Output: Ping Pong

Overloading () Must be a member function Any number of parameters can be specified Any return type can be specified Operator() can perform any generic operation

class String{ ... public: char & operator()(int); }; Function Operator class String{ ... public: char & operator()(int); };

Function Operator char & String::operator() (int pos){ assert(pos>0 && pos<=size); return bufferPtr[pos-1]; }

Subscript Operator int main(){ } String s1(“Ping”); char g = s1(2); // g = ‘i’ s1(2) = ‘o’; cout << g << “\n”; cout << str.GetString(); return 0; }

Function Operator Output: i Pong

class String{ ... public: String operator()(int, int); }; Function Operator class String{ ... public: String operator()(int, int); };

Function Operator String String::operator()(int index, int subLength){ assert(index>0 && index+subLength-1<=size); char * ptr = new char[subLength+1]; for (int i=0; i < subLength; ++i) ptr[i] = bufferPtr[i+index-1]; ptr[subLength] = ‘\0’; String str(ptr); delete [] ptr; return str; }

Function Operator int main(){ String s(“Hello World”); // “<<“ is overloaded cout << s(1, 5); return 0; }

Function Operator Output: Hello

Unary operators: & * + - ++ -- ! ~ Examples: --x -(x++) !(*ptr ++)

Unary Operators Unary operators are usually prefix, except for ++ and -- ++ and -- both act as prefix and postfix Example: h++; g-- + ++h - --i;

Unary Operators General syntax for unary operators: Member Functions: TYPE & operator OP (); Non-member Functions: Friend TYPE & operator OP (TYPE & t);

Unary Operators Overloading unary ‘-’: class Complex{ ... Complex operator - (); // friend Complex operator // -(Complex &); }

Unary Operators Member function definition: Complex Complex::operator -(){ Complex temp; temp.real = -real; temp.img = -img; return temp; }

Unary Operators Unary ‘+’ is overloaded in the same way Complex c1(1.0 , 2.0), c2; c2 = -c1; // c2.real = -1.0 // c2.img = -2.0 Unary ‘+’ is overloaded in the same way