Date post: | 26-Dec-2015 |
Category: |
Documents |
Upload: | emily-charles |
View: | 227 times |
Download: | 0 times |
Operator Operator OverloadingOverloading
Customised behaviour of Customised behaviour of operatorsoperators
Unit - 06Unit - 06
Unit IntroductionUnit Introduction
This unit covers operator This unit covers operator overloadingoverloading
Unit ObjectivesUnit Objectives
After covering this unit you will After covering this unit you will understand…understand…
Operator overloadingOperator overloading Different types of operator and their Different types of operator and their
overloadingoverloading Operators that cannot be overloadedOperators that cannot be overloaded Inheritance and overloadingInheritance and overloading Automatic type conversionAutomatic type conversion
Syntax and OverviewSyntax and Overview
Operator overloading used for Operator overloading used for customised behaviour of different customised behaviour of different operatorsoperators
Declared using Declared using operator@operator@ keyword, keyword, where @ represents the operator where @ represents the operator that’s being overloadedthat’s being overloaded
Example: Operator Example: Operator OverloadingOverloading
class OverloadingExample class OverloadingExample
{{
private:private:
int m_LocalInt;int m_LocalInt;
public:public:
OverloadingExample(int j) OverloadingExample(int j) // default constructor// default constructor
{{
m_LocalInt = j;m_LocalInt = j;
}}
int operator+ (int j) int operator+ (int j) // overloaded + operator// overloaded + operator
{{
return (m_LocalInt + j);return (m_LocalInt + j);
}}
};};
Example: Operator Overloading Example: Operator Overloading (contd.)(contd.)
void main()void main()
{{
OverloadingExample object1(10);OverloadingExample object1(10);
cout << object1 + 10; cout << object1 + 10; // overloaded operator called// overloaded operator called
}}
Types of OperatorTypes of Operator
Unary operatorUnary operator Binary operatorBinary operator
Unary OperatorsUnary Operators
Operators attached to a single Operators attached to a single operand (-a, +a, --a, a--, ++a, a++)operand (-a, +a, --a, a--, ++a, a++)
Example: Unary Example: Unary OperatorsOperators
class UnaryExampleclass UnaryExample
{{
private:private:
int m_LocalInt;int m_LocalInt;
public:public:
UnaryExample(int j)UnaryExample(int j)
{{
m_LocalInt = j;m_LocalInt = j;
}}
int operator++ () int operator++ ()
{{
return (m_LocalInt++);return (m_LocalInt++);
}}
};};
Example: Unary Operators Example: Unary Operators (contd.)(contd.)
void main()void main()
{{
UnaryExample object1(10);UnaryExample object1(10);
cout << object1++; cout << object1++; // overloaded operator results in value // overloaded operator results in value // 11// 11
}}
Binary OperatorsBinary Operators
Operators attached to two operands Operators attached to two operands (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, a>=b, a<b, a<=b, a==b)a<b, a<=b, a==b)
Example: Binary Example: Binary OperatorsOperators
class BinaryExample 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)int operator+ (BinaryExample& rhsObj)
{{
return (m_LocalInt + rhsObj.m_LocalInt);return (m_LocalInt + rhsObj.m_LocalInt);
}}
};};
Example: Binary Operators Example: Binary Operators (contd.)(contd.)
void main()void main()
{{
BinaryExample object1(10), object2(20);BinaryExample object1(10), object2(20);
cout << object1 + object2; cout << object1 + object2; // overloaded operator called// overloaded operator called
}}
Non-Overloadable Non-Overloadable OperatorsOperators
Operators that can not be Operators that can not be overloaded due to safety reasons:overloaded due to safety reasons: Member Selection ‘.’ operatorMember Selection ‘.’ operator Member dereference ‘.*’ operatorMember dereference ‘.*’ operator Exponential ‘**’ operatorExponential ‘**’ operator User-defined operatorsUser-defined operators Operator precedence rulesOperator precedence rules
Operator Overloading and Operator Overloading and InheritanceInheritance
An operator is overloaded in super An operator is overloaded in super class but not overloaded in derived class but not overloaded in derived class is called non-member operator class is called non-member operator in derived classin derived class
In above, if operator is also overloaded In above, if operator is also overloaded in derived class it is called member-in derived class it is called member-operatoroperator
= ( ) [ ] –> –>* operators must be = ( ) [ ] –> –>* operators must be member operatorsmember operators
Other operators can be non-member Other operators can be non-member operatorsoperators
Automatic Type Automatic Type ConversionConversion
Automatic type conversion by the Automatic type conversion by the C++ compiler from the type that C++ compiler from the type that doesn’t fit, to the type it wantsdoesn’t fit, to the type it wants
Two types of conversion:Two types of conversion: Constructor conversionConstructor conversion Operator conversionOperator conversion
Constructor ConversionConstructor Conversion
Constructor having a single Constructor having a single argument of another type, results in argument of another type, results in automatic type conversion by the automatic type conversion by the compilercompiler
Prevention of constructor type Prevention of constructor type conversion by use of conversion by use of explicitexplicit keywordkeyword
Example: Constructor Example: Constructor ConversionConversion
class One class One
{{
public:public:
One() {}One() {}
};};
class Two class Two
{{
public:public:
Two(const One&) {}Two(const One&) {}
};};
void f(Two) {}void f(Two) {}
void main()void main()
{{
One one;One one;
f(one); f(one); // Wants a Two, has a One// Wants a Two, has a One
}}
Operator ConversionOperator Conversion
Create a member function that takes Create a member function that takes the current type the current type
Converts it to the desired type using Converts it to the desired type using the the operator operator keyword followed by the keyword followed by the type you want to convert totype you want to convert to
Return type is the Return type is the namename of the of the operator overloadedoperator overloaded
Reflexivity - global overloading instead Reflexivity - global overloading instead of member overloading; for code savingof member overloading; for code saving
Example: Operator Example: Operator ConversionConversion
class Threeclass Three
{{
int m_Data;int m_Data;
public:public:
Three(int ii = 0, int = 0) : m_Data(ii) {}Three(int ii = 0, int = 0) : m_Data(ii) {}
};};
class Four class Four
{{
int m_Data;int m_Data;
public:public:
Four(int x) : m_Data(x) {}Four(int x) : m_Data(x) {}
operator Three() const operator Three() const
{ {
return Three(m_Data); return Three(m_Data);
}}
};};
void g(Three) {}void g(Three) {}
Example: Operator Conversion Example: Operator Conversion (contd.)(contd.)
void main()void main()
{{
Four four(1);Four four(1);
g(four);g(four);
g(1); g(1); // Calls Three(1,0)// Calls Three(1,0)
}}
Type Conversion PitfallsType Conversion Pitfalls
Compiler performs automatic type Compiler performs automatic type conversion independently, therefore conversion independently, therefore it may have the following pitfalls:it may have the following pitfalls: Ambiguity with two classes of same Ambiguity with two classes of same
typetype Automatic conversion to more than one Automatic conversion to more than one
type - type - fan-outfan-out Adds hidden activities (copy-constructor Adds hidden activities (copy-constructor
etc)etc)
Unit SummaryUnit Summary
In this unit you have covered …In this unit you have covered … Operator overloadingOperator overloading Different types of operatorDifferent types of operator Operators that cannot be overloadedOperators that cannot be overloaded Inheritance and overloadingInheritance and overloading Automatic type conversionAutomatic type conversion