Operator overloading

Post on 06-May-2015

1.208 views 2 download

Transcript of Operator overloading

I N T R O D U C T I O N

O P E R A T O R O V E R L O A D I N G R E S T R I C T I O N

G E N E R A L R U L E S F O R O V E R L O A D I N G O P E R A T O R

O V E R L O A D I N G U N A R Y O P E R A T O R

O V E R L O A D I N G B I N A R Y O P E R A T O R

Operator Overloading

Compiled By: Kamal Acharya

Introduction

It gives additional meaning to the C++ operator when applied to the user defined data types.

We can create our own language by using the concept of operator overloading appropriately.

It require great care when misused program became very difficult to understand

Compiled By: Kamal Acharya

Operator Overloading Restriction

Following C++ Operator can’t be overloaded

Class member access operators(. & .*)

Scope Resolution Operator(::)

Sizeof Operator(sizeof())

Conditional Operator(? :)

Precedence of an operator cannot be changed

Associativity of an operator cannot be changed

Arity (number of operands) cannot be changed

Unary operators remain unary, and binary operators remain binary

Operators &, *, + and - each have unary and binary versions

Unary and binary versions can be overloaded separately

Compiled By: Kamal Acharya

No new operators can be created Use only existing operators

No overloading operators for built-in types Cannot change how two integers are added

Compiled By: Kamal Acharya

General Rules for overloading Operator

Syntax:

returnType classname::operator op(arguments)

{

body;

}

Example

void Integer::operator op()

{

Body;

}

Compiled By: Kamal Acharya

Steps

1. Create a class that is to be used

2. Declare the operator function in the public part of the class. It may be either member function or friend function.

3. Define operator function to implement the operation required

4. Overloaded can be invoked using the syntax such as:

op x;

Compiled By: Kamal Acharya

Overloading Unary Operator

Unary operator are those operator which works only on the single operand. Eg. ++, --, - etc

Unary operator acts on only one operand and can be overloaded in two ways:

1. Using non-static member function with no arguments

2. Using friend function with one argument where the argument must be either an object of the class or an reference to an object of the class

Compiled By: Kamal Acharya

Using non static member function

Example

class Test

{

…………

public:

void operator op()

{……… }

};

Void main()

{

…….

…….

op obj1; /* Same as obj1.operator op()*/

………………..

}

Compiled By: Kamal Acharya

Sample Program

#include<iostream.h>

#include<conio.h>

class increment

{

int m,n;

public:

increment(int x, int y)

{

m=x;

n=y;

}

void display()

{

cout<<"m= "<<m<<

"n="<<n<<endl;

}

void operator ++()

{

m++;n++;

}

};

Compiled By: Kamal Acharya

void main()

{

clrscr();

increment in1(20,30);

in1.display();

++in1;

in1.display();

increment in2(1,2);

in2.display();

in2.operator ++();

in2.display();

getch();

}

Compiled By: Kamal Acharya

OUTPUT

Compiled By: Kamal Acharya

Using Friend Function

Example

class Test

{

…………

public:

friend void operator op(Test);

};

Void main()

{

…….

…….

op obj1; /* Same as operator op(obj1)*/

………………..

}

Compiled By: Kamal Acharya

Sample Program

#include<iostream.h>

#include<conio.h>

class increment

{

int m,n;

public:

increment(int x, int y)

{

m=x; n=y;

}

void display()

{

cout<<"m= "<<m<<

"n="<<n<<endl;

}

friend void operator ++(increment&);

};

Compiled By: Kamal Acharya

void operator ++(increment& x)

{

++x.m;

++x.n;

}

void main()

{

clrscr();

increment in1(20,30);

in1.display();

++in1;

in1.display();

increment in2(1,2);

in2.display();

operator ++(in2);

in2.display();

getch();

}

Compiled By: Kamal Acharya

OUTPUT

Compiled By: Kamal Acharya

Overloading Binary Operator

Binary operator are those operator which works on two operands. Eg. +, -,*,/ etc

Binary operator acts on two operands and can be overloaded in two ways:

1. Using non-static member function with single argument.

2. Using friend function with two arguments where the arguments must be either an object of the class or an reference to an object of the class.

Compiled By: Kamal Acharya

Using non static member function

Example

class Test

{

…………

public:

void operator op(Test)

{……… }

};

Void main()

{

…….

…….

obj1 op obj2; /* Same as obj1.operator op(obj2)*/

………………..

}

Compiled By: Kamal Acharya

Sample Program

#include<iostream.h>

#include<conio.h>

class add

{

int m,n;

public:

add(int x, int y)

{

m=x; n=y;

}

void display()

{

cout<<"m= "<<m<<" n="<<n<<endl;

}

void operator +(add);

};

Compiled By: Kamal Acharya

void add::operator +(add x)

{

m=m+x.m;

n=n+x.n;

}

void main()

{

clrscr();

add obj1(20,30),obj2(2,3);

obj1.display();

obj2.display();

obj1+obj2;

obj1.display();

getch();

}

Compiled By: Kamal Acharya

Compiled By: Kamal Acharya

Using Friend Function

Example

class Test

{

…………

public:

friend void operator op(Test, Test);

};

Void main()

{

…….

…….

obj1 op obj2; /* Same as operator op(obj1, obj2)*/

………………..

}

Compiled By: Kamal Acharya

Sample Program

#include<iostream.h>

#include<conio.h>

class add

{

int m,n;

public:

add(int x, int y)

{

m=x; n=y;

}

void display()

{

cout<<"m= "<<m<<" n="<<n<<endl;

}

friend void operator +(add&,add&);

};

Compiled By: Kamal Acharya

void operator +(add& x, add& y)

{

x.m=x.m+y.m;

x.n=x.n+y.n;

}

void main()

{

clrscr();

add obj1(20,30),obj2(2,3);

obj1.display();

obj2.display();

obj1+obj2;

obj1.display();

getch();

}

Compiled By: Kamal Acharya

Compiled By: Kamal Acharya