Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.

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.
Operator Overloading Fundamentals
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.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 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 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
Chapter 13: Overloading.
ISBN Lecture 07 Expressions and Assignment Statements.
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.
Expressions, Evaluation and Assignments
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 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.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Operator Overloading in C++. Operator Overloading It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Expressions and Assignment Statements
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.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
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.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignments Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Programming with ANSI C ++
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Expressions and Assignment Statements
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Operator Overloading; String and Array Objects
Object Oriented Programming COP3330 / CGS5409
Operator Overloading BCA Sem III K.I.R.A.S.
Arithmetic Expressions
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Operator Overloading.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Operator Overloading.
Chapter 7 Expressions and Assignment Statements.
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

Function and Operator Overloading

Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler can figure out which definition to use by the number and type of arguments Operator overloading is giving additional definitions to operators Some operators that can be overloaded +, -, >, <, [ ], *, etc

The difference between a function and an operator An operator (such as +) is really just a function with different syntax In a function, the argument are placed in parentheses after the function name, as in: –add(cost, tax) –This is postfix notation For an operator, the argument are placed on either side of the operator, as in: –cost + tax –This is infix notation

Advantages of operator overloading The point of operator overloading is to provide the same concise expression for user-defined types as built in types It is just clearer and simpler to say –Cost + tax, than to say –Add(cost, tax)

The role of types in overloading C++ programming is a type sensitive and type- focused process Programmers can use built-in types or define their own types Operators can be used with user-defined types as well as built-in types –The operators new meaning must be defined The compiler uses the type of the arguments to decide which definition to use for overloaded functions or operators

How operators are overloading You write a function (with a header and body) as you normally would The function name is now the keyword operator followed by the symbol to be overloaded –Example: return_type operator++( ) To use an operator on a class object, that operator must be overloaded (with a couple of exceptions) –The assignment operator (=) –The address operator (&)

Restrictions on operator overloading The precedence of an operator cannot be changed by overloading. –An aside on precedence—table on precedence You cannot change the number of operands(arguments) an operators takes –Unary operators remain unary; binary remain binary; the only ternary operator (:?) cannot be overloaded It is not possible to create new operators –So ** for exponentiation cannot be created

Operator functions as class members vs. as friend functions They can be either class members or friends As member functions: –The leftmost (or only) argument must be a class object or reference to a class object If the left operand must be a built-in type or object of a different class, it must be a non- member function –If it must access private members of the class directly, then it must be a friend function

Data Conversions Type conversions between built-in data types may be implicit or explicit –Use static_cast (old_type_variable) for explicit type conversions All conversions between objects and built-in types must be explicit Example: convert 5 feet 3 inches to 5.25 feet this is a conversion from Distance to a float