Operator Overloading Enable C++’s operators to work with class object >, +, -, *, / This operators perform differently depending on their context in integer,

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
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Operator Overloading (2) Operator function can be implemented as class member function Then the left most (or only) operand must be an class object( or.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
ADT Specification Example
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
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.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
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.
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.
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
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.
CS Object Oriented Programming Using C++
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
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.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 16
Andy Wang Object Oriented Programming in C++ COP 3330
Overloading C++ supports the concept of overloading Two main types
Function Overloading Can enables several function Of same name
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
Visit for more Learning Resources
Object Oriented Programming COP3330 / CGS5409
Object-Oriented Design (OOD) and C++
Polymorphism in C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 16
Jim Fawcett Copyright ©
Chapter 15: Overloading and Templates
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
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++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator overloading Friend Function This Operator Inline Function
Jim Fawcett Copyright ©
Presentation transcript:

Operator Overloading Enable C++’s operators to work with class object >, +, -, *, / This operators perform differently depending on their context in integer, floating These operators are overloaded C++ allow programmer to overload most operators

Can we make Complex class easier to use as follow? int main() { Complex x,y,z; cout<<“Type in two complex numbers\n”; cin>>x >>y; z = x + y; cout<< x <<“+” <<y<<“=“ <<z<<endl; } Type in two complex numbers (2,3) (3,4) (2,3) + (3,4) = (5,7)

Yes, Overload the operators Operator are overloaded by writing a function definition( header and body) Function name become the keyword operator followed by the symbol for the operator being overloaded operator+ would be used to overload the addition operator(+) Precedence and associativity of an operator cannot be changed by overloading

Where to define overloading operator As non member function Must be friend of the class So it is in class definition class Complex { friend ostream& operator<< (ostream&, const Complex &);... } Function name Return type parameters

Implementation ostream & operator<<(ostream & out, const Complex &c) { out<<“(“<<c.real<<“,”<<c.img<<“)”; return out; // enables cout<<x<<y; }

Overload operator >> In class definition class Complex { friend ostream& operator<< (ostream &, const Complex &); friend istream& operator>> (istream&, Complex &);... }

implementation istream & operator>>( istream & input,Complex & c) { input.ignore(); input>>c.real; input.ignore(); input>>c.img; input.ignore(); return input; }

Operator Overloading Operator function can be implemented as class member function Then the left most (or only) operand must be an class object( or a reference to a class object) If the left operand must be a object of a different class or build-in type, this operator function must be implemented as a non-member function( as we did for >>, <<)

Arithmetic operators +, -, *, /, +=, -=, *=, /= In header class Complex {friend ostream& operator<< (ostream &, const Complex &); friend istream& operator>> (istream&, Complex &); Complex operator+(const Complex &c,)const;... }

implementation Complex Complex::operator+( Complex &c) { Complex sum; sum.real = real + c.real; sum.img = img+ c.img; return sum; }

In header Complex operator-(const Complex& c); Implementation Complex Complex::operator-(const complex &c) { return Complex(real-c.real, img=c.img); }

In header Complex operator*(const Complex& c); Implementation Complex Complex::operator*(const Complex &c) { Complex mul; mul.real = real*c.real-img*c.img; mul.img = real*c.img + img+c.img; return mul; }

In header Complex operator/(const Complex& c); Implementation Complex Complex::operator*(const Complex &c) { Complex div; double dem; dem = c.real*c.real + c.img+c.img; div.real = (real*c.real+img*c.img)/dem; div.img = (img+c.img -real*c.img )/dem; return div; }