1 CSC241: Object Oriented Programming Lecture No 08.

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Operator Overloading Fundamentals
1 Overloading Operators COSC 1567 C++ Programming Lecture 7.
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 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.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Chapter 13: 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 Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
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. 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.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
1 CSC241: Object Oriented Programming Lecture No 13.
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.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
1 CSC241: Object Oriented Programming Lecture No 12.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
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.
Chapter 8 Operator Overloading, Friends, and References.
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.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Recap……Last Time [Variables, Data Types and Constants]
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
CSE1002 – Problem Solving with Object Oriented Programming
COMP 53 – Week Two Operator Overloading.
Chapter 13: Overloading and Templates
Operator Overloading; String and Array Objects
Visit for more Learning Resources
CSC241: Object Oriented Programming
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Operator Overloading, Friends, and References
Operator Overloading.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 08

2 Previous Lecture Abstract data type (ADT) Container classes Proxy classes Operator overloading

3 Today’s Lecture Operator overloading Overloading unary operator o ++ o - - Argument of overloaded function – Post increment

4 Operator overloading For example, statements like – d3.addobjects(d1, d2); or the similar but equally obscure – d3 = d1.addobjects(d2); can be changed to the much more readable – d3 = d1 + d2;

5 Examples of c++ overloaded operator Addition + and subtraction – operator  +, – operators perform differently, depending on their context in integer arithmetic, floating-point arithmetic and pointer arithmetic. Stream insertion ( >) operator (cout >)  << is used both as the stream insertion operator and as the bitwise left-shift operator  >> is used both as the stream extraction operator and as the bitwise right-shift operator  > are overloaded in the C++ Standard Library std

6 Syntax of overloaded operator function An operator is overloaded by writing a non-static member function definition Function name becomes the keyword operator followed by the symbol for the operator being overloaded – E.g. operator + () { …. } Function name operator+ would be used to overload the addition operator (+) Assignment operator (=) may be used with every class to perform member wise assignment of the data members – d1 = d2;

7 Precedence, Associativity and No. of Operands Precedence means which operator to solve first (+, -, *, /, = ) The precedence of an operator cannot be changed by overloading The associativity of an operator cannot be changed by overloading Overloaded unary operators (++, --) remain unary operators overloaded binary operators remain binary operators

8 Creating New Operators It is not possible to create new operators only existing operators can be overloaded – E.g. ** can be used for exponential in some programming languages – ** is not in the list od existing operator so it cannot be overloaded Attempting to create new operators via operator overloading is a syntax error

9 Operators for Fundamental Types The meaning of how an operator works on fundamental types cannot be changed by operator overloading For example, programmer cannot change the meaning of how + adds two integers Operator overloading works only with – objects of user-defined types or – a mixture of an object of a user-defined type and an object of a fundamental type

10 Examples c1++; or c1 --; – Overloading ++ and -- unary operator dist3 = dist1 + dist2; – Overloading assignment and addition binary operator – It does not mean that += is also overloaded for Distance objects dist1+= dist2; or dist1 - =dist2

11 Overloading Unary Operators Examples of unary operators are the increment and decrement operators ++ and --, and the unary minus, as in -33 Counter class example – To keep track of a count. – Objects of that class were increment the count value by calling a member function: c1.inc_count(); But it would be more readable if we could have used the increment operator ++ ++c1;

12 Counter class Example class Counter{ private: unsigned int count; //count public: Counter() : count(0) //constructor { } unsigned int get_count() //return count { return count; } void operator ++ (){ //increment (prefix) ++count; } }; Program Output main(){ Counter c1, c2; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count(); ++c1; ++c2; ++c2; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count() << endl; } c1 = 0 c2 = 0 c2 = 2 c1 = 1 0 count c1 0 count c Go to program

13 The operator Keyword The keyword operator is used to overload the ++ operator void operator ++ () The return type (void in this case) comes first, followed by the keyword operator, followed by the operator itself (++), and finally the argument list enclosed in parentheses This declarator syntax tells the compiler to call this member function whenever the ++ operator is encountered

14 Cont. The compiler can distinguish between overloaded functions in the following ways – data types of arguments and – the number of their arguments The only way a compiler can distinguish between overloaded operators is by looking at the data type of their operands. – If the operand is a basic type such as an int then the compiler will use its built-in routine to increment an int

15 Operator Arguments ++ operator is applied to a specific object, as in the expression ++c1 What does this operator increment? It increments the count data in the object of which it is a member Since member functions can always access the particular object for which they’ve been invoked, this operator requires no arguments

16 Operator Return Values c1 = ++c2; Not possible Why ?? ++ operator is defined to have a return type of void in the operator++() function, while in the assignment statement it is being asked to return a variable of type Counter The normal ++ operator, applied to basic data types such as int, would not have this problem int x, y; y=10; x = ++ y;

17 class Counter{ unsigned int count; public: Counter() : count(0) { } unsigned int get_count() { return count; } Counter operator ++ (){ ++count; Counter temp; temp.count = count; return temp; } }; Counter class Example main() { Counter c1, c2; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count(); ++c1; c2 = ++c1; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count() ; } 0 count c1 0 count c Program Output c1=0 c2=0 c2=2 c1=2 ++c1; c2 =c1; Go to program

18 Nameless Temporary Objects main() { Counter c1, c2; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count(); ++c1; c2 = ++c1; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count() ; } class Counter{ unsigned int count; public: Counter() : count(0) { } Counter(int c) : count(c) { } unsigned int get_count() { return count; } Counter operator ++ (){ ++count; return Counter(count); } }; Counter temp; temp.count = count; return temp;

19 Postfix Notation So far we’ve seen the increment operator used only in its prefix form. ++c1 What about postfix, where the variable is incremented after its value is used in the expression? c1++

20 Counter class Example class Counter{ private: unsigned int count; public: Counter() : count(0) { } Counter(int c) : count(c) { } unsigned int get_count() const { return count; } Counter operator ++ (){ return Counter(++count); } Counter operator ++ (int){ return Counter(count++); } }; main(){ Counter c1, c2; cout << “c1=” << c1.get_count(); cout << “c2=” << c2.get_count(); ++c1; c2 = ++c1; cout << “c1=” << c1.get_count(); cout << “c2=” << c2.get_count(); c2 = c1++; cout << “c1=” << c1.get_count(); cout << “c2=” << c2.get_count() ; } 0 count c1 0 count c Program Output c1=0 c2=0 c2=2 c1=2 ++c1;c2 = ++c1; c2 = c1++; 23 c2=2 c1=3 Go to program