OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.

Slides:



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

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
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 Customised behaviour of operators Unit - 06.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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.
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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
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;
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 CSC241: Object Oriented Programming Lecture No 08.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Operator Overloading Ritika Sharma.
Object Oriented Programming COP3330 / CGS5409
Polymorphism in C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 21
Object-Oriented Programming (OOP) Lecture No. 16
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading CSCE 121 J. Michael Moore
Operators Lecture 10 Fri, Feb 8, 2008.
Overview of C++ Overloading
Operator Overloading, Friends, and References
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++
Operator Overloading I
Operator overloading Friend Function This Operator Inline Function
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Presentation transcript:

OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3

OOP Spring 2007 – Recitation 32 Today: Operator overloading Friend This

OOP Spring 2007 – Recitation 33 Operator Overloading

OOP Spring 2007 – Recitation 34 The “Old” Matrix Consider class Matrix that implements a 2D matrix of integers. It supports all the usual operations – arithmetics, printing, indexing, assigning. Its use looks like: Matrix m1, m2, m3; m3.copy(m1); m3.add(m2); m3.print(); cout << m1.index(5, 5);

OOP Spring 2007 – Recitation 35 The “New” Matrix It’s much more natural to use standard notation: +, *, <<, = … These are called operators, and in C++ the programmer can define new meanings for them – this is called operator overloading. Now the usage is more natural: Matrix m1, m2, m3; m3 = m1 + m2; cout << m3; cout << m1(5, 5);

OOP Spring 2007 – Recitation 36 Syntax An overloaded operator is simply a function with funny name and calling syntax. It can implement any behavior we want ( + can subtract, etc.) Name for the function that implements + is operator+() and it can be called either by m1 + m2; or by operator+(m1, m2); Basically, the compiler replaces the former form by the latter.

OOP Spring 2007 – Recitation 37 Two Forms of An Operator Just as any function, an operator can be defined as a non-member function, or as a class method. If a compiler sees m1 + m2; it looks for operator+ in m1 ’s class and writes m1.operator+(m2); and for non-member operator+ and writes operator+(m1, m2); If both exist, overloading resolution is used.

OOP Spring 2007 – Recitation 38 Binary and Unary A binary operator takes two arguments (such as +, -, < ). A unary operator takes one argument (such as ++, ! ).

OOP Spring 2007 – Recitation 39 Overloadable Binary These binary operators can be overloaded: +-*/% &|^ =+=-=*=/=%=<<=>>=&=|=^= <<=>>===!= []() ->*->, <<>>&&||

OOP Spring 2007 – Recitation 310 Overloadable Unary These unary operators can be overloaded: ~!-+ &*newnew[]deletedelete[] ++ (prefix)++ (postfix)-- (prefix)-- (postfix)

OOP Spring 2007 – Recitation 311 Non-overloadable These operators cannot be overloaded: ::..*sizeoftypeid?:

OOP Spring 2007 – Recitation 312 Binary Looks and Calling A binary operator can be defined by either –a non-static member function taking one argument of any type, –or a non-member function taking two arguments of any type. Return type can be anything. For a binary can be interpreted either as or as b).

OOP Spring 2007 – Recitation 313 Unary Looks and Calling A unary operator can be defined by either –a non-static member function taking no arguments, –or a non-member function taking one argument of any type. Return type can be anything. For a can be interpreted either as or as

OOP Spring 2007 – Recitation 314 Must-be Members These operators must be non-static member functions of a class: – operator= – operator[] – operator() – operator-> This is to ensure that their first operand is lvalue. All the rest can be either member or non- member.

OOP Spring 2007 – Recitation 315 Friends

OOP Spring 2007 – Recitation 316 Non-member Private Access Sometimes we need to “break” the encapsulation of a class – allow a non- member function access to private parts. For example, operator+() should not be a member function, but it might need access to implementation. C++ allows the class to declare some function as its friend, thus allowing access to private parts.

OOP Spring 2007 – Recitation 317 Friend Function Adding keyword friend before function name in a class definition implies that this function is friend of the class rather than member. The function still needs to be defined. class Complex { friend Complex operator+(const Complex&, const Complex&); }; operator+() above is not a member function of Complex (we can’t call c.operator+() for Complex c ), but it can access private parts of Complex.

OOP Spring 2007 – Recitation 318 Friend Class A class can declare another class’s method as friend : class A { friend void B::stop(); }; class B { void stop(); }; If all methods of one class need to be friend s of another, the whole class can be declared friend : class A { friend class B; };

OOP Spring 2007 – Recitation 319 Technicalities friend ship is not transitive. A friend of a friend is not (necessarily) a friend. It doesn’t matter if the friend declaration is made in the public or private parts (public is better). If the friend function is itself a method of another class, it doesn’t matter if it is in the public or private parts. Don’t declare everything friend just because you can.

OOP Spring 2007 – Recitation 320 This

OOP Spring 2007 – Recitation 321 The pointer this Class Date day month year set() ge() d1.set(10,3,2000) d2.set(5,4,2007) void Date::set(int d, int m, int y) { day = d; month = m; year = y; }

OOP Spring 2007 – Recitation 322 The pointer this How the compiler can know which fields to update in set() ? d1’s or d2’s ? Answer: The compiler does a “translation” of the code to a c-like code. It adds as the beginning of the parameter list a pointer to the object, named this

OOP Spring 2007 – Recitation 323 The pointer this void Date::set(Date* const this, int d, int m, int y) { this->day = d; this->month = m; this->year = y; } d1.set(10,3,2000)  Date::set(&d1,10,3,2000)

OOP Spring 2007 – Recitation 324 The pointer this We can use this explicitly Sometimes we must use it for example: when a member function should return the object it is working on (as in operators overloading)