Operator Overloading Customised behaviour of operators Unit - 06.

23
Operator Operator Overloading Overloading Customised behaviour of Customised behaviour of operators operators Unit - 06 Unit - 06

Transcript of Operator Overloading Customised behaviour of operators Unit - 06.

Page 1: Operator Overloading Customised behaviour of operators Unit - 06.

Operator Operator OverloadingOverloading

Customised behaviour of Customised behaviour of operatorsoperators

Unit - 06Unit - 06

Page 2: Operator Overloading Customised behaviour of operators Unit - 06.

Unit IntroductionUnit Introduction

This unit covers operator This unit covers operator overloadingoverloading

Page 3: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 4: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 5: Operator Overloading Customised behaviour of operators Unit - 06.

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);

}}

};};

Page 6: Operator Overloading Customised behaviour of operators Unit - 06.

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

}}

Page 7: Operator Overloading Customised behaviour of operators Unit - 06.

Types of OperatorTypes of Operator

Unary operatorUnary operator Binary operatorBinary operator

Page 8: Operator Overloading Customised behaviour of operators Unit - 06.

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++)

Page 9: Operator Overloading Customised behaviour of operators Unit - 06.

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++);

}}

};};

Page 10: Operator Overloading Customised behaviour of operators Unit - 06.

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

}}

Page 11: Operator Overloading Customised behaviour of operators Unit - 06.

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)

Page 12: Operator Overloading Customised behaviour of operators Unit - 06.

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);

}}

};};

Page 13: Operator Overloading Customised behaviour of operators Unit - 06.

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

}}

Page 14: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 15: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 16: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 17: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 18: Operator Overloading Customised behaviour of operators Unit - 06.

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

}}

Page 19: Operator Overloading Customised behaviour of operators Unit - 06.

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

Page 20: Operator Overloading Customised behaviour of operators Unit - 06.

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) {}

Page 21: Operator Overloading Customised behaviour of operators Unit - 06.

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)

}}

Page 22: Operator Overloading Customised behaviour of operators Unit - 06.

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)

Page 23: Operator Overloading Customised behaviour of operators Unit - 06.

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