In this article Operator Overloading in C++ we give the information about A Operator which contain only one operand is called unary operator overloading.

Operator Overloading in C++:

Define Overloading Overloading:

“To assign more than one operation on an same operator known as operator overloading.”

Overloading we have to write a special function know as operator().

Operator Overloading Syntax:

return_type operator operator_symbol(argument_list)

{

statement(s);

}

you can write operator() in two ways-

  1. class function
  2. friend function

In C++, most of the operator’s can be overloaded, but there are some operator’s in it which cannot be overloaded. Operator’s which cannot be overloaded are the following:-

scope operator – ::

sizeof

member selector -.

member pointer selector – *

ternary operator -?:

Rules of Operator Overloading:–

There are some important rules for this which we have to keep in mind.

  • Only built-in operators can be overloaded. If some operator is not present in C++ then we cannot overload them.
  • The precedence and associatively of operators cannot be changed.
  • We cannot use the friend function to overload any particular operators. But, we can use the member function to overload these operators.
  • It is necessary to define assignment “=”, subscript “[]”, function call “()” and arrow operator “->” as member function.
  • The overloaded operator must include at least one operand of the user-defined data type.
  • An overloaded operator cannot hold default parameters.
  • Some operators such as – assignment “=”, address “&”, and comma “,” are already overloaded.

Type of Overloading:

There  are two types of Overloading such as follows

  1. Function overloading
  2. Operator Overloading

Operator Overloading:

There  are two types of operator overloading in C++ such as

  1. Unary Operator Overloading
  2. Binary Operator Overloading

Operator Overloading in C++

1. Unary Operator Overloading:

 A Operator which contain only one operand is called unary operator overloading.

 Syntax:

1. Class Function

return_type operator operator_symbol()

{

statement(s);

}

2. Friend Function: 

friend return_type operator operator_symbol()

{

statement(s);

}

Unary Operator overloading Program:

// Unary Operator Overloading – Using Class function 
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b;
public:
demo(int x, int y)
{
a=x;
b=y;
}
void show()
{
cout<<“\n A: “<<a<<“\t B: “<<b;
}
void operator -()
{
a=-a;
b=-b;
}
};
void main()
{
clrscr();

int p,q;

cout<<“\n Enter the two numbers:  “;

cin>>p>>q;
demo obj (p,q);
obj.show();
-obj;
obj.show();
getch();
}

O/P:-

Enter the two numbers: 10

20

A: -10    B: -20

// Unary Operator Overloading – Friend function 
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b;
public:
demo(int x, int y)
{
a=x;
b=y;
}
void show()
{
cout<<“\n A: “<<a<<“\t B: “<<b;
}
friend void operator -(demo &obj);

};
void operator -(demo &obj)
{
obj.a=-obj.a;
obj.b=-obj.b;
}
void main()
{
clrscr();
int p,q;
cout<<“\n Enter the two numbers: “;
cin>>p>>q;
demo obj (p,q);
obj.show();
-obj;
obj.show();
getch();
}

O/P:-

Enter the two numbers: -10

-20

A: 10    B: 20

2. Binary Operator Overloading:-

A operator which contain two operands is called Binary Operator Overloading.

Syntax:

1. Class Function

return_type operator operator_symbol(Argument List)

{

statement(s);

}

// Binary Operator Overloading – Using Class function 
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b;
public:
demo()
{

}

demo(int x, int y)
{
a=x;
b=y;
}
void show()
{
cout<<“\n A: “<<a<<“\t B: “<<b;
}
demo operator +(demo obj)
{
demo temp;
temp.a=a+obj.a;
temp.b=b+obj.b;
return temp;
}

};
void main()
{
clrscr();
demo ob1(5,6), ob2(10,20), ob;
ob=ob1+ob2;
ob.show();
getch();
}

O/P:

A: 15        B: 26

2. Friend Function: 

friend return_type operator operator_symbol(Argument List)

{

statement(s);

}

// Binary Operator Overloading
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b;
public:
demo()
{

}

demo(int x, int y)
{
a=x;
b=y;
}
void show()
{
cout<<“\n A: “<<a<<“\t B: “<<b;
}
friend demo operator +(demo &obj, demo &obj2);

};
demo operator +(demo &obj, demo &obj2)
{
demo temp;
temp.a= obj.a+obj2.a;
temp.b= obj.b+obj2.b;
return temp;
}

void main()
{
clrscr();
demo obj(5,6); demo ob1(10,20); demo ob2;
ob2=obj+ob1;
ob2.show();
getch();
}

O/P:

A: 15        B: 26

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *