OPERATOR OVERLOADING Customised behaviour of operators

Preview:

Citation preview

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

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

Restrictions on Operator Overloading

Most of C++’s operators can be overloaded

Operators that can be overloaded

+ - * / % ^ & |

~ ! = < > += -= *=

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

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

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

new[] delete[]

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

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

Coding Practices

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

}

};

Example: Operator Overloading (contd.)

void main()

{

OverloadingExample object1(10);

cout << object1 + 10;

}

Types of Operator

Unary operator Binary operator

Unary Operators

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

Example: Unary Operators

class UnaryExample

{

private:

int m_LocalInt;

public:

UnaryExample(int j)

{

m_LocalInt = j;

}

int operator++ ()

{

return (m_LocalInt++);

}

};

Example: Unary Operators (contd.)

void main()

{

UnaryExample object1(10);

cout << object1++;

}

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

Binary Operators

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

Example: Unary Operators

class UnaryExample

{

private:

int m_LocalInt;

public:

UnaryExample(int j)

{

m_LocalInt = j;

}

int operator++ ()

{

return (m_LocalInt++);

}

};

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)

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

}

};

Example: Binary Operators (contd.)

void main()

{

BinaryExample object1(10), object2(20);

cout << object1 + object2;

}

Another Way of Doing it!

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

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

void main(){ BinaryExampleU object1(10),

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

object4.print();}

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 !=

Thankyou

Recommended