24
OPERATOR OVERLOADING Customised behaviour of operators

OPERATOR OVERLOADING Customised behaviour of operators

Embed Size (px)

Citation preview

Page 1: OPERATOR OVERLOADING Customised behaviour of operators

OPERATOR OVERLOADING

Customised behaviour of operators

Page 2: OPERATOR OVERLOADING Customised behaviour of operators

Introduction

Operator overloading Use traditional operators with user-defined

objects Straightforward and natural way to extend

C++ Requires great care

When overloading misused, program difficult to understand

Page 3: OPERATOR OVERLOADING Customised behaviour of operators

Fundamentals of Operator Overloading Use operator overloading to improve

readability Avoid excessive or inconsistent usage

Format Write function definition as normal Function name is keyword operator

followed by the symbol for the operator being overloaded.

operator+ would be used to overload the addition operator (+)

Page 4: OPERATOR OVERLOADING Customised behaviour of operators

Restrictions on Operator Overloading

Most of C++’s operators can be overloaded

Operators that can be overloaded

+ - * / % ^ & |

~ ! = < > += -= *=

/= %= ^= &= |= << >> >>=

<<= == != <= >= && || ++

-- ->* , -> [] () new delete

new[] delete[]

Page 5: OPERATOR OVERLOADING Customised behaviour of operators

Restrictions on Operator Overloading (II)

Arity (number of operands) cannot be changed Urnary operators remain urnary, and binary

operators remain binary Operators *, + and - each have unary and

binary versions Unary and binary versions can be overloaded

separately

Page 6: OPERATOR OVERLOADING Customised behaviour of operators

Restrictions on Operator Overloading (III)

No new operators can be created Use only existing operators

Built-in types Cannot overload operators You cannot change how two integers are

added

Page 7: OPERATOR OVERLOADING Customised behaviour of operators

Coding Practices

Page 8: OPERATOR OVERLOADING Customised behaviour of operators

Example: Operator Overloadingclass OverloadingExample

{ private:

int m_LocalInt;

public:

OverloadingExample(int j) // default constructor

{

m_LocalInt = j;

}

int operator+ (int j) // overloaded + operator

{

return (m_LocalInt + j);

}

};

Page 9: OPERATOR OVERLOADING Customised behaviour of operators

Example: Operator Overloading (contd.)

void main()

{

OverloadingExample object1(10);

cout << object1 + 10;

}

Page 10: OPERATOR OVERLOADING Customised behaviour of operators

Types of Operator

Unary operator Binary operator

Page 11: OPERATOR OVERLOADING Customised behaviour of operators

Unary Operators

Operators attached to a single operand (-a, +a, --a, a--, ++a, a++)

Page 12: OPERATOR OVERLOADING Customised behaviour of operators

Example: Unary Operators

class UnaryExample

{

private:

int m_LocalInt;

public:

UnaryExample(int j)

{

m_LocalInt = j;

}

int operator++ ()

{

return (m_LocalInt++);

}

};

Page 13: OPERATOR OVERLOADING Customised behaviour of operators

Example: Unary Operators (contd.)

void main()

{

UnaryExample object1(10);

cout << object1++;

}

Page 14: OPERATOR OVERLOADING Customised behaviour of operators

Prefix and Postfix Notation

int operator++ () // Prefix{ return (++m_LocalInt);}

int operator++ (int) // postfix{ return (m_LocalInt++);}Only difference is the int in the parenthesis

Page 15: OPERATOR OVERLOADING Customised behaviour of operators

Binary Operators

Operators attached to two operands a+b, a-b, a*b etc.)

Page 16: OPERATOR OVERLOADING Customised behaviour of operators

Example: Unary Operators

class UnaryExample

{

private:

int m_LocalInt;

public:

UnaryExample(int j)

{

m_LocalInt = j;

}

int operator++ ()

{

return (m_LocalInt++);

}

};

Page 17: OPERATOR OVERLOADING Customised behaviour of operators

Binary Operators

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)

Page 18: OPERATOR OVERLOADING Customised behaviour of operators

Example: Binary Operators

class BinaryExample

{

private:

int m_LocalInt;

public:

BinaryExample(int j)

{

m_LocalInt = j;

}

int operator+ (BinaryExample rhsObj)

{

return (m_LocalInt + rhsObj.m_LocalInt);

}

};

Page 19: OPERATOR OVERLOADING Customised behaviour of operators

Example: Binary Operators (contd.)

void main()

{

BinaryExample object1(10), object2(20);

cout << object1 + object2;

}

Page 20: OPERATOR OVERLOADING Customised behaviour of operators

Another Way of Doing it!

How to solve object1 + object2 +object3+……?

Page 21: OPERATOR OVERLOADING Customised behaviour of operators

class BinaryExampleU{ private:

int m_LocalInt; public: BinaryExampleU(int j)

{ m_LocalInt = j;

}BinaryExampleU operator+ (BinaryExampleU rhsObj){ return BinaryExampleU(m_LocalInt +

rhsObj.m_LocalInt);}

void print() {

cout<<m_LocalInt; }};

Page 22: OPERATOR OVERLOADING Customised behaviour of operators

void main(){ BinaryExampleU object1(10),

object2(20), object3 (30), object4(0); object4 = object1 + object2 +object3 ;

object4.print();}

Page 23: OPERATOR OVERLOADING Customised behaviour of operators

Case Study: An Array class

Implement an Array class with Range checking Array assignment Arrays that know their size Outputting/inputting entire arrays with <<

and >> Array comparisons with == and !=

Page 24: OPERATOR OVERLOADING Customised behaviour of operators

Thankyou