Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемЕкатерина Журавская
1 Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
2 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
3 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
4 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.
7 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)
8 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)
9 Operators that can be overloaded +-*/%^ &|~!=< >+=-=*=/=%= ^=&=|=<<>>>>= <<===!=<=>=&& ||++--->*,-> []()newdeletenew[]delete[]
10 Types of Operators OPERATORS OPERATORS Unary Binary (+, <, =, …) Prefix (!, &, ~, …) Postfix (++, --, …)
11 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; }
12 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++
13 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; }
14 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; }
17 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
18 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; }
19 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
20 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
21 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; }
22 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.
23 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
24 Conversion Between C-String and String Objects
25 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
26 Lecture Summary Lectures, books and so on will be updated at: (
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.