operator overloading

24
INTRODUCTION OPERATOR OVERLOADING RESTRICTION GENERAL RULES FOR OVERLOADING OPERATOR OVERLOADING UNARY OPERATOR OVERLOADING BINARY OPERATOR Operator Overloading Compiled By: Kamal Acharya

Upload: kamal-acharya

Post on 06-May-2015

1.209 views

Category:

Education


2 download

TRANSCRIPT

Page 1: 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

Page 2: Operator overloading

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

Page 3: Operator overloading

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

Page 4: Operator overloading

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

Page 5: Operator overloading

General Rules for overloading Operator

Syntax:

returnType classname::operator op(arguments)

{

body;

}

Example

void Integer::operator op()

{

Body;

}

Compiled By: Kamal Acharya

Page 6: Operator overloading

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

Page 7: Operator overloading

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

Page 8: Operator overloading

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

Page 9: Operator overloading

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

Page 10: Operator overloading

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

Page 11: Operator overloading

OUTPUT

Compiled By: Kamal Acharya

Page 12: Operator overloading

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

Page 13: Operator overloading

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

Page 14: Operator overloading

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

Page 15: Operator overloading

OUTPUT

Compiled By: Kamal Acharya

Page 16: Operator overloading

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

Page 17: Operator overloading

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

Page 18: Operator overloading

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

Page 19: Operator overloading

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

Page 20: Operator overloading

Compiled By: Kamal Acharya

Page 21: Operator overloading

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

Page 22: Operator overloading

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

Page 23: Operator overloading

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

Page 24: Operator overloading

Compiled By: Kamal Acharya