Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
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.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Introduction to Programming Lecture 39. Copy Constructor.
Operator Overloading Fundamentals
Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Software Design and C++ Programming Lecture 4 Operator Overloading and Streamed I/O.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
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),
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
EE4E. C++ Programming Lecture 4 Operator Overloading and Streamed I/O.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
 2008 Pearson Education, Inc. All rights reserved 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.
CS Object Oriented Programming Using C++
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
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 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Learners Support Publications Constructors and Destructors.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Constructors and Destructors
Introduction to Programming
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
Lecture 9: Operator Overloading
Operator Overloading; String and Array Objects
Operator Overloading.
Advanced Program Design with C++
Constructors and Destructors
Object-Oriented Programming (OOP) Lecture No. 20
Operator Overloading.
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++
Chapter 14 Object-Oriented Software Development
Operator Overloading; String and Array Objects
IT533 Adapted from Textbook (SAVITCH) and Lecture Notes (PUSATLI), 2014.
Presentation transcript:

Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects of a class by providing an operator function. Examples of those that can not be overloaded are ::,., and ?: Operators keep their predefined precedence and associatively class X{public: X operator -(); // unary -ve operator invoked by X a; -a; X operator ++(); // prefix increment invoked by X a; ++a; X operator ++(int); // postfix increment, X a; a++; X operator -(X&); // binary -, X b; a - b ; X operator +(X&); // add operator, a + b; // The return type X is needed to cascade operators // global functions can also be used to overload operators on objects

Operator Overloading A simple example: class Complex{ double real, imag; public: Complex(double r=0.0, double I=0.0){real = r; imag = I;} Complex operator ++(){real += 1.0; imag += 1.0; return *this;} Complex operator +(Complex& c){ Complex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; // this invokes the default copy constructor // can also be implemented in one line as return Complex(real + c.real, imag + c.imag); } }; main(){ Complexc1(1.5,2.0), c2(1.2,2.2), c3; c3 = ++c1 + ++c2; //why the ++ functions must return a Complex data type

Operator Overloading What happens when the assignment exists as in operators +=, -=, *= assume we will add the following member functions to class Complex Complex& operator +=(Complex&); Complex& operator -=(Complex&); Complex& operator *=(Complex&); Complex& Complex::operator +=(Complex& a){ real += a.real; imag += a.imag return *this;} Complex& Complex::operator *=(Complex& a){ real = real*a.real - imag * a.imag; // there is an error here imag = real * a.imag + imag * a.real; return *this;}

Operator Overloading The indexing operator [] for an array class Vector{ float *p; int size; public: Vector(int n){ p = new float[n]; size=n;} ~Vector() {delete p;} float&operator [](int I){return p[I];} Vectoroperator +(const Vector & a){ Vector temp(size); for(int I=0; I<size; I++) temp.p[I] = p[I] + a.p[I]; return temp; // this will destroy the resulting Vector why? // a default copy constructor will be used to copy the attributes p and size // only // To remedy this, a special copy constructor must be defined as follows, Vector(Vector& c){p = new float[c.size], size = c.size; for(int I=0; I<size; I++) p[I] = c.p[I]; }// this will form a new dynamic object for the result

Operator Overloading The assignment operator Although the assignment operator is supported by a default copy operator, the following example will produce a Null pointer assignment fun(){ Vector x1(20), x2(10); x2 = x1; // now x2.p and x1.p have the same address, size = 20 }// the destructor function will be called once for x1 and once for x2 // upon function exit, the same array will be deleted twice The following assignment operator function is needed to remedy this Vector& operator = (const Vector& a){ if(this != &a){// check that this is not x=x delete p; // clean up p = new float [size = a.size]; for(int I =0;I<size;I++) p[I] = a.p[I];} return *this;} // every class should contain a copy constructor and an assignment operator

Operator Overloading Non-member operator functions used to define operations between different types of objects e.g. Vector operator * (const Matrix& a, const Vector& b){ Vector temp; for(int I = 0; I < a.no_rows; I++) for(int j = 0; j < a.no_cols; j++) temp[I] = a[I][j] * b[I]; return temp; } The above function should be declared as friend function in both class matrix and class vector as follows, class Matrix{…friend Vector operator * (const Matrix&,const Vector&);} class Vector{…friend Vector operator * (const Matrix&,const Vector&);}

Operator Overloading Overloading the stream input/output operators >> and << this is needed to apply these operators to the instances of objects e.g., Complex x(0.0,0.0); cin >> x; cout << x; // this needs the following functions which input or outputs complex numbers as (flt.pt, flt.pt.) istream& operator >> (istream& input, const Complex& a){ double re=0,im=0; char c; input >> c; // input a character in c if(c == ‘(‘ ){ input >> re >> c; // input real part followed by, if ( c == ‘,’ ) {input >> im >> c; // input imag part followed by ) if ( c == ‘)’ ){ a = Complex(re,im); return input;}}} cout << “error in input format”; } ostream& operator << (ostream& output, const Complex& a){ return output << ‘\n’ << ‘(‘ << a.real << ‘,’ << a.imag << ‘)’ << ‘\n’; }// the above functions must be declared as friends in class Complex