Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Operator Overloading Fundamentals
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
Operator overloading Object Oriented Programming.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
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
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
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.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©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.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading CS 3370 – C++ Chapter 14.
Overloading C++ supports the concept of overloading Two main types
Chapter 13: Overloading and Templates
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
C# Operator Overloading and Type Conversions
Visit for more Learning Resources
Object-Oriented Programming (OOP) Lecture No. 21
Jim Fawcett Copyright ©
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
Overview of C++ Overloading
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Operator Overloading I
Operator Overloading; String and Array Objects
Jim Fawcett Copyright ©
Presentation transcript:

Operator Overloading

Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement –Smart pointers (operator ->) –Function call

Complex Numbers A Canonical Example –Rational is another Complex numbers are pairs of real numbers (real, imaginary), e.g., (2,3), (1.04, -12.4) like points in the x-y plane (a, b) + (c, d) = (a+c, b+d) applications in Electrical Engineering Compare function-style operations to using operator functions…

A complex Class #include class complex { double real; double imag; public: complex(double real=0, double imag=0) { this->real = real; this->imag = imag; } complex add(const complex& c) const { complex result; result.real = this->real + c.real; result.imag = this->imag + c.imag; return result; } void display(std::ostream& os) const { os << '(' << real << ',' << imag << ')'; } };

Using the complex Class using namespace std; int main() { complex z(1,2), w(3,4); complex a = z.add(w);// want a = z+w a.display(cout); } (4,6)

Operator Functions operator keyword together with an operator operator+ operator- etc. Can overload all operators except: ::..* ?:

complex with operator+ class complex { //... public: //... complex operator+(const complex& c) const { complex result; result.real = this->real + c.real; result.imag = this->imag + c.imag; return result; } //... }; int main() { complex z(1,2), w(3,4); complex a = z + w;// z.operator+(w) a.display(cout); }

Rules operator+ is the function name you can also invoke it directly as z.operator+(w) a binary function, of course Normal precedence rules apply Can be either a global or member function If member, this is the left operand If global, at least one argument must be user- defined

Conversion Constructors and Operator Overloading Single-arg constructors provide implicit conversions –For example T::T(int) Can use 2 initialization syntaxes: T t(1); T t = 1; Provide implicit conversions t + 1 becomes t + T(1) Can disable with the explicit keyword

complex Conversion class complex { public: complex(double real = 0, double imag = 0) { this->real = real; this->imag = imag; } // … }; int main() { complex w(3,4); complex a = w + 1;// w + (1,0) a.display(cout); } (4,4)

Member vs. Non-member Operators The expression 1 + w won’t compile. Left-operand must be a class object to match a member operator A global operator will apply an implicit conversion to either operand via a single-arg constructor, if available In general, binary operators should be non-members –For math operators, anyway In general, unary operators should be members

Global complex Operators class complex { public: complex(double real = 0, double imag = 0) { this->real = real; this->imag = imag; } double getReal() const {return real;} double getImag() const {return imag;} //... }; complex operator+(const complex& c1, const complex& c2) { double real = c1.getReal() + c2.getReal(); double imag = c1.getImag() + c2.getImag(); complex result(real, imag); return result; }

A Unary complex Operator class complex { public: complex operator-() const { return complex(-real, -imag); } // … }; int main() { complex w(3,4); complex a = -w; a.display(cout); } (-3,-4)

Stream Operators operator > Must be global –because left operand is a stream! Stream is passed by reference –for efficiency Should also return the stream –to support chaining insertions and extractions

complex Stream Output ostream& operator<<(ostream& os, const complex& c) { os << '(' << c.getReal() << ',’ << c.getImag() << ')'; return os; } int main() { complex w(3,4); complex a = -w; cout << a << endl; } (-3,-4)

A “Complete” complex Would provide all pertinent operators –including assignment ops such as +=, -=, etc. –assignment ops must be members –typically implement + in terms of +=, etc. Provide stream insertion and extraction Global functions are class friends –not necessary, just a convenience

operator[] A Unary Operator –And takes an integer argument, of course Must be a member For “array-like” things –vectors, strings Usually should provide two versions: –version for const objects –version for non-const objects –other operators may require this pattern

A “Safe” Array class class Index { enum {N = 100}; int data[N]; int size; public: Index(int n) { if (n > N) throw "dimension error"; for (int i = 0; i < n; ++i) data[i] = i; size = n; } int getSize() const {return size;} int& operator[](int i) { if (i = size) throw "index error"; return data[i]; } };

Using Index #include using namespace std; int main() { Index a(10); for (int i = 0; i < a.getSize(); ++i) cout << a[i] << ' '; cout << endl; a[5] = 99; cout << a[5] << endl; cout << a[10] << endl; } abnormal program termination

Using a const Index #include using namespace std; int main() { const Index a(10);// a const Index for (int i = 0; i < a.getSize(); ++i) cout << a[i] << ' ';// COMPILE ERROR! cout << endl; }

Supporting a const Index class Index { //... int& operator[](int i) { if (i = size) throw "index error"; return data[i]; } int operator[](int i) const// A const version { if (i = size) throw "index error"; return data[i]; } };

operator[][] Doesn’t exist! –Just like multi-dimensional arrays don’t exist Simulate it –Using the “array of arrays” paradigm a[i][j] == (a.operator[](i)).operator[](j) –The outer class operator[] should return something that has an operator[] to invoke Example: op2db.cpp

Conversion Operators The complement to single-arg constructors Provide implicit conversions to another type Member function with the signature: operator T() const;

Index-to-double Conversion class Index { //... operator double() const { double sum = data[0]; for (int i = 1; i < size; ++i) sum += data[i]; return sum / size; } }; int main() { const Index a(10); double x = a + 1; cout << x << endl; } 5.5

Warning! Why shouldn’t the complex class have a conversion operator to int or double ? Hint: consider the expression w + 1 where w is complex. You can turn off implicit conversions of single-arg constructors with the explicit keyword: explicit complex(double = 0, double = 0);

lvalue vs. rvalue Detection Idiom For operator[ ] –Because bits are not individually addressable –Setting and resetting a bit are very different operations Uses a Proxy class –Returned by bitset ::operator[] Stores the bit position and a reference to the bitset –Overloads operator=(bool) –Converts implicitly to bool

Inspect bits.h Old Code –Before templates Data on stack “reference” proxy class Bit offset computations

Stream State if (stream) tests true if the stream state is good How is this accomplished? Two choices: –operator void* (historical) –operator bool Examples: opbool.cpp, opvoid.cpp

Other Operators -> for “smart pointers” ++, -- –Pre and post versions ( ), “function-call” operator –We’ll see this when we do STL

Overloading operator-> For when you want to add functionality to the built- in operator-> Must return a raw pointer –Or something that can be dereferenced Example: SafePtr.cpp

Overloading ++ and -- Must distinguish between pre and post –Post versions take an extraneous int argument The post versions must save current value –That’s why the pre versions are more efficient –They should also return a const object To disallow x++++ –Illegal, modifies temporary Examples: PrePost.cpp, SafeArrayPtr.cpp

Overloading operator() The Function Call Operator Constitutes a Function Object –An object that can behave like a function –Compensates for C++ not being Lisp! If class T::operator() exists: –Then t( ) acts like a function call Example: findGreater.cpp, findGreater2.cpp

Operator Overloading  Checkpoint   Why does C++ support operator overloading?  When should you make an operator function a member?  When should you make an operator function a non-member?  What are the differences between member and non-member operators?  Why should you provide two versions of operator[] ?  When should you use a conversion operator?  When shouldn’t you use a conversion operator?