You are on page 1of 7

OPERATOR OVERLOADING

In computer programming, operator overloading (less


commonly known as operator ad-hoc polymorphism) is a specific case of
polymorphism in which some or all of operators like +, =, or == have
different implementations depending on the types of their arguments.
Sometimes the overloading are defined by the language; sometimes the
programmer can implement support for new types.

Operator overloading is claimed to be useful because it allows the


developer to program using notation "closer to the target domain"[1] and
allows user-defined types a similar level of syntactic support as types built
into the language. It can easily be emulated using function calls; for an
example, consider the integers a, b, c:

a+b*c

In a language that supports operator overloading, and assuming the


'*' operator has higher precedence than '+', this is effectively a more
concise way of writing:

Operations on Boolean Operands:


a) op ∨ = (bool a, b) bool:( a | true | b );
b) op ∧ = (bool a, b) bool: ( a | b | false );
c) op ¬ = (bool a) bool: ( a | false | true );
d) op = = (bool a, b) bool:( a∧b ) ∨ ( ¬b∧¬a );
e) op ≠ = (bool a, b) bool: ¬(a=b);
f) op abs = (bool a)int: ( a | 1 | 0 );

C++ provides more than 35 operators, covering basic arithmetic,


bit manipulation, indirection, comparisons, logical operations and others.
Almost all operators can be overloaded for user-defined types, with a few
notable exceptions such as member access (. and .*). The rich set of over
loadable operators is central to using C++ as a domain specific language.
The overload able operators are also an essential part of many advanced
C++ programming techniques, such as smart pointers. Overloading an
operator does not change the precedence of calculations involving the
operator, nor does it change the number of operands that the operator
uses (any operand may however be ignored by the operator, though it will
be evaluated prior to execution). Overloaded "&&" and "||" operators lose
their short-circuit evaluation property.

By using increment operator

# include<iostream.h>

# include<conio.h>
class count{

private:

int n;

public:

count()

n=0;

void show ()

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

void operator ++()

n=n+1;

};

void main()

clrscr();

count obj;

obj.show();

++obj;

obj.show();

getch();

OUTPUT: N=0 N=1

By using increment operator with return value

# include<iostream.h>

# include<conio.h>

class count

private:

int n;

public:
count()

n=0;

void show ()

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

count operator ++()

count temp;

n=n+1;

temp.n=n;

return temp;

};

void main()

clrscr();

count x, y;

x.show();

y.show();

y=++x;

x.show();

y.show();

getch()

OUTPUT:

N=0 N=1

N=0 N=1

By using postfix increment

# include<iostream.h>

# include<conio.h>

class count

{
private:

int n;

public:

count()

n=0;

void show ()

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

count operator ++()

count temp;

n=n+1;

temp.n=n;

return temp;

};

void main()

clrscr();

count x;

x.show();

++x;

x++;

x.show();

getch();

OUTPUT: N=2 N=0

Binary operator overloading

# include<iostream.h>

# include<conio.h>

class add

{
private:

int a,b;

public:

add()

a=b=0;

void in()

cout<<"enter a:";

cin>>a;

cout<<"enter b:";

cin>>b;

void show()

cout<<"a="<<a<<endl;

cout<<"b="<<b<<endl;

add operator +(add p)

add temp;

temp.a=p.a+a;

temp.b=p.b+b;

return temp;

};

void main()

add x,y,z;

x.in();

y.in();

z=x+y;

x.show();

y.show();

z.show();
getch();

OUTPUT:

Enter a: 5

Enter b: 6

Enter a: 8

Enter b: 9

A=5

B=6

A=8

B=9

A=13

B=15

Arithmetic addition operator

# include<iostream.h>

# include<conio.h>

# include<string.h>

# include<stdio.h>

class string

private:

char str[50];

public:

string()

str[0]='\0';

void in()

cout<<"enter string:";

gets(str);

void show()

{cout<<str<<endl;

}
string operator = (string s)

string temp;

strcpy(temp.str,str);

strcat(temp.str,s.str);

return temp;

};

void main()

string s1,s2,s3;

s1.in();

s2.in();

cout<<"s1=";

s1.show();

cout<<"s2=";

s2.show();

cout<<"s3=";

s3.show();

cout<<"concatenating s1 and s2 in s3..."<<endl;

s3 = s1+s2;

cout<<"s3=";

s3.show();

getch();

OUTPUT:

Enter stering: Sohaib

Enter string: jamil

concatenating s1 and s2 in s3...

s1= sohaob

s2= jamil

s3= sohaibjamil

You might also like