Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date: 24.09.2012.

Презентация:



Advertisements
Похожие презентации
Unit II Constructor Cont… Destructor Default constructor.
Advertisements

Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
1/13 Chapter 06- Implementing Operators in a class.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 2.
1/27 Chapter 9: Template Functions And Template Classes.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
1 © Luxoft Training 2012 Inner and anonymous classes.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 1.
Michael Marchenko. In mathematics, a sequence is an ordered list of objects (or events). Like a set, it contains members (also called elements, or terms),
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Customer-to-Provider Connectivity with BGP Connecting a Multihomed Customer to Multiple Service.
1 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Транксрипт:

Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:

Objectives Overloading in C++ Overloading in C++ Function overloading Function overloading Operator overloading Operator overloading Different types of operators and their overloading Different types of operators and their overloading Operators that cannot be overloaded Operators that cannot be overloaded Data conversion Data conversion Automatic type conversion Automatic type conversion User-defined type conversion User-defined type conversion

C++ Overloading Overloading in C++ allows to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. Overloading in C++ allows to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is the one that had been declared with exactly the same name as the previous declaration in the same scope, except that both declarations have different arguments and also different definition (implementation). An overloaded declaration is the one that had been declared with exactly the same name as the previous declaration in the same scope, except that both declarations have different arguments and also different definition (implementation). Function Operator C++ OVERLAODING

C++ Function Overloading An overloaded function can have multiple definitions for the same function name in the same scope. An overloaded function can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. Function declarations cannot be overloaded if they differ only by return type. Function declarations cannot be overloaded if they differ only by return type.

C++ Operator Overloading It simplifies the program listing, e.g., It simplifies the program listing, e.g., d3.addobjects(d1, d2) or the similar but equally obscure d3 = d1.addobjects(d2) can be changed to much more readable form d3 = d1 + d2 Operator overloading refers to giving normal C++ operators such as +, *, and <= so on, an additional meaning when they are applied to user defined data types, e.g., Operator overloading refers to giving normal C++ operators such as +, *, and <= so on, an additional meaning when they are applied to user defined data types, e.g., d3 = d1 + d2 (legal when d1, d2, and d3 are basic types) d3 = d1 + d2 (legal when d1, d2, and d3 are basic types)

C++ Operator Overloading (Syntax) returnType operator*(parameters); any type keyword operator symbol any type keyword operator symbol Return type may be whatever the operator returns Return type may be whatever the operator returns Including a reference to the object of the operand Including a reference to the object of the operand Operator symbol may be any valid operator allowed by the language compiler (see the following list) Operator symbol may be any valid operator allowed by the language compiler (see the following list)

Operators that can be overloaded +-*/%^ &|~!=< >+=-=*=/=%= ^=&=|=<<>>>>= <<===!=<=>=&& ||++--->*,-> []()newdeletenew[]delete[]

Types of Operators OPERATORS OPERATORS Unary Binary (+, <, =, …) Prefix (!, &, ~, …) Postfix (++, --, …)

Example: Operator Overloading class OverloadingExample { private: int m_LocalInt; int m_LocalInt; public: public: OverloadingExample(int j) // default constructor { m_LocalInt = j;} int operator+ (int j) // overloaded + operator { return (m_LocalInt + j);} }; int main() { OverloadingExample object1(10); cout << object1 + 10; // overloaded operator called getch(); return 0; }

Unary Operators Operators attached to a single operand, Operators attached to a single operand, e.g., -a, +a, --a, a--, ++a, a++ e.g., -a, +a, --a, a--, ++a, a++

Example: Unary Operators (Prefix) class UnaryExample { private: int m_LocalInt; int m_LocalInt; public: public: UnaryExample(int j) UnaryExample(int j) { m_LocalInt = j;} int operator++ () { return (++m_LocalInt);} }; int main() { UnaryExample object1(10); cout << ++object1; // overloaded operator cout << ++object1; // overloaded operatorgetch(); return 0; }

Example: Unary Operators (Postfix) class UnaryExample { private: int m_LocalInt; int m_LocalInt; public: public: UnaryExample(int j) UnaryExample(int j) { m_LocalInt = j;} int operator++ (int) // int argument for postfix operator { return m_LocalInt++; } }; int main() { UnaryExample object1(10); cout << object1++; // overloaded operator cout << object1++; // overloaded operatorgetch(); return 0; }

Binary Operators Operators attached to two operands, Operators attached to two operands, e.g., e.g., a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b a<b, a<=b, a==b

Example: Binary Operators class BinaryExample { private: private: int m_LocalInt; int m_LocalInt; public: public: BinaryExample(int j) BinaryExample(int j) { m_LocalInt = j; } { m_LocalInt = j; } int operator+ (BinaryExample& rhsObj) { return (m_LocalInt + rhsObj.m_LocalInt); } }; int main() { BinaryExample object1(10), object2(20); cout << object1 + object2; // overloaded operator called cout << object1 + object2; // overloaded operator calledgetch(); return 0; }

Non-Overloadable Operators Operators that cannot be overloaded due to safety reasons: Operators that cannot be overloaded due to safety reasons: Member Selection. operator Member Selection. operator Member dereference.* operator Member dereference.* operator Exponential ** operator Exponential ** operator User-defined operators User-defined operators Operator precedence rules Operator precedence rules

Data Conversion Assignment operator assigns a value from one side to another, e.g., Assignment operator assigns a value from one side to another, e.g., intvar1 = intvar2 But what happens when the variables on different sides of the = sign are of different types? But what happens when the variables on different sides of the = sign are of different types? Two possibilities: Two possibilities: Automatic data conversion Automatic data conversion User-defined data conversion User-defined data conversion

Conversion Between basic Types #include using namespace std; int main() { int intvar; float floatvar; //casting provides explicit conversion intvar = static_cast (floatvar); //casting provides explicit conversion getch(); return 0; }

Conversion between User-defined and Basic Types Built-in conversion routines cant be relied while converting b/w user-defined data types and basic types; since the compiler doesnt know anything about user-defined types besides what we tell it. Built-in conversion routines cant be relied while converting b/w user-defined data types and basic types; since the compiler doesnt know anything about user-defined types besides what we tell it.

Create a member function that takes the current type Create a member function that takes the current type Converts it to the desired type using the operator keyword followed by the type you want to convert to. Converts it to the desired type using the operator keyword followed by the type you want to convert to. Return type is the name of the operator overloaded Return type is the name of the operator overloaded Reflexivity - global overloading instead of member overloading; for code saving. Reflexivity - global overloading instead of member overloading; for code saving. Syntax: Syntax: operator type_name() operator type_name() {} {} Conversion between User-defined and Basic Types

Conversion Between C-String and String Objects

Lecture Summary Lecture covered … Overloading in C++ Overloading in C++ Function overloading Function overloading Operator overloading Operator overloading Different types of operator Different types of operator Operators that cannot be overloaded Operators that cannot be overloaded Data conversion: Data conversion: Automatic type conversion Automatic type conversion User-defined type conversion User-defined type conversion

Lecture Summary Lectures, books and so on will be updated at: (