Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Spring Semester 2013 Lecture 5
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
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.
Chapter 14: Overloading and Templates
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Chapter 8 Operator Overloading, Friends, and References.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Controlling Program Flow with Decision Structures.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 CSC241: Object Oriented Programming Lecture No 08.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
CSE1002 – Problem Solving with Object Oriented Programming
EGR 2261 Unit 11 Pointers and Dynamic Variables
User-Written Functions
Object Oriented Programming COP3330 / CGS5409
Operator Overloading.
Pointers and Pointer-Based Strings
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading
Pointers and Pointer-Based Strings
Review of Previous Lesson
Constructors and Deconstructor
Presentation transcript:

Chapter 8 Operator Overloading

 Operator overloading is considered one of the more useful techniques for improving readability and ease of programming in your code.  It can transform complex, obscure program listings into more obvious ones.

Operator Overloading  For example:  d3.addobjects(d1, d2);  Or similar but still harder to read:  d3 = d1.addobjects(d2);  All those can now be changed to the much easier to read code:  d3 = d1 + d2;

Operator Overloading  The fancy word operator overloading just refers to giving the normal C++ operators, such as +, *,<=, +=, more meaning when they are applied to user-defined data types.  Normally a = b + c; works only with basic types such as int and float.

Operator Overloading  If you tried to apply a = b + c; if a, b, and c were objects of a user-defined class, the compiler would complain.  However when you use overloading, you can make this statement legal even when a, b, and c are user-defined types.

Operator Overloading  In effect, operator overloading gives you the opportunity to redefine the C++ language.  If you find yourself limited by way the C++ operators work, you can change them to do whatever you want.  By using classes and operator overloading, now you can extend C++ to be in many ways a new language of your own design.

Overloading Unary Operators  Let’s start off by overloading the unary operator.  Unary operators only operate on one operand.  Examples of unary operators are the increment and decrement operators ++ and --, and the unary minus, as in –33.

Overloading Unary Operators  When we studied classes, we looked at a counter class to keep track of a count.  Objects of that class were incremented by calling a member function:  c1.inc_count();  That statement did the job correctly but it would be more readable if we used the ++ operator instead.  ++c1;

Overloading Unary Operators  The statement ++c1 can be understood by just about any programmer.  So let’s rewrite our old Counter example to make it possible to use the ++c1 operator.  Let’s look at “countpp1.cpp”.

The operator Keyword  How do we teach a normal C++ operator to act on a user-defined operand?  The keyword operator is used to overload the ++ operator in this declarator:  void operator ++ ()

The operator Keyword  void operator ++ ()  The return type (void in this case) come first, followed by the keyword operator, followed by the operator itself (++), and finally the argument list enclosed in parentheses (which are empty here).  This declarator syntax tells the compiler to call this member function whenever the ++ operator is encountered, provided the operand (the variable operated on by the ++) is of type Counter.

The operator Keyword  We saw in Chapter 5, “Functions” that the only way the compiler can distinguish between overloaded functions is by looking at the data types and the number of their arguments.  In the same way, the only way it can distinguish between overloaded operators is by looking at the data type of their operands.

The operator Keyword  If the operand is a basic type such as an int, as in  ++intvar;  Then the compiler will use its built-in routine to increment an int.  But if the operand is a Counter variable, the compiler will know to use our user- written operator++() instead.

Operator Arguments  In main() the ++ operator is applied to a specific object, as in the expression ++c1.  However the operator++() takes no arguments.  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.

Operator Return Values  The operator++() function in the countpp1.cpp program has a subtle defect.  You will see this problem if you try to use the statement:  c1 = ++c2;  When the compiler see this statement, it will complain, why?

Operator Return Values  Because we have defined the ++ operator to have a return type of void in the operator++() function, while the assignment statement it is being asked to return a variable of type Counter.  Again, the compiler is being asked to return whatever value c2 has after being operated on by the ++ operator, and assign this value to c1.

Operator Return Values  So as defined in countpp1.cpp, we can’t use ++ to increment Counter object in assignments; it must always stand alone with its operand.  Of course the normal ++ operator, applied to basic data types such as int, would not have this problem.

Operator Return Values  To make it possible to use our homemade operator++() in assignment expressions, we must provide a way for it to return a value.  The next program “countpp2.cpp” does just that.

Nameless Temporary Objects  In coutpp2.cpp we created a temporary object of type Counter, named temp, whose sole purpose was to provide a return value for the ++ operator.  This requires 3 statements:  counter temp;  temp.count = count;  return temp;

Nameless Temporary Objects  There are more convenient ways to return temporary objects from functions and overloaded operators.  Let’s look at an example of this in “countpp3.cpp”.