Operator Overloading in C++ with example



The process of redefining the meaning of the operator for a specific purpose within a program is known as operator overloading.
For example, the function for the operator + can be written that defines concatenation of two string objects.
Polymorphism is a generic concept that involves operator and function overloading. It can be used across several classes where the same function name handle may be used for different types of object.

Overloadable /Non-overloadable Operators:

Following is the list of operators which can be overloaded:

+
-
*
/
%
^
&
|
~
!
,
=
<=
>=
++
--
<< 
>> 
==
!=
&&
||
+=
-=
/=
%=
^=
&=
|=
*=
<<=
>>=
[]
()
->
->*
new
new []
delete
delete []

Following is the list of operators, which cannot be overloaded:

::
.*
.
?:

In the C++ mostly all operator can be overloading but there are some operator cannot overload.
1.      :: Scope Resolution
2.      Size of operator
3.      Condition operator, ternary
4.      -> indirection operator

Two types of Overloading
1.      Unary Overloading and
2.      Binary Overloading


There are Unary and Binary function objects. A Unary function object requires one argument;
A binary function object requires two arguments.

Example of Unary Overloading

#include<iostream.h>
#include<conio.h>
class unary
               {
                              int a, b;
                              public:
                              void get ();
                              void put ();
                              void operator – ();
               };
void unary :: get ()
{
               cout<<”Enter the number a and b = ”;
               cin>>a>>b;
}
void unary :: put()
{
               cout<<”a = ”;
               cout<<a;
               cout<<”b = ”;
               cout<<b;
}
void unary :: operator – ()
{
               this -> a = this -> a – 1;
               this -> b = this -> b – 1;
}
void main ()
{
               clrscr ();
unary u, v;
               u.get ();
               u.put ();
               - u;
               u.put ();
               v.get ();
               v.put ();
               - v;
               v.put ();
               getch();
}

Input:
Enter the number a and b = 2 3

Output:
a =2 b = 3
a =1 b = 2

Input:
Enter the number a and b = 4 5

Output:
a =4 b = 5
a =3 b = 4

Example of Binary Overloading

#include<iostream.h>
#include<conio.h>
class complex
               {
                              int r, i;
                              public:
                              void get ();
                              void put ();
                              complex operator + (complex t)
               };
void complex :: get ()
{
               cout<<”Enter the number = ”;
               cin>>r>>i;
}
void complex :: put()
{
               cout<<”Real = ”<<r<<”+”<<”Imaginary =”<<i;
}
complex complex :: operator + (complex t)
{
               complex temp;
               temp.r = r + t.r;
               temp.i = I + t.i;
               return temp;
}
void main ()
{
clrscr ();
complex st, mt, kt;
st.get ();
mt.get ();
kt = st + mt;
st.put ();
mt.put ();
kt.put ();
getch ();
}

Input:
Enter the number = 3 4
Enter the number = 4 4

Output:
Real = 3 Imaginary = 4
Real = 4 Imaginary = 4
Real = 7 Imaginary = 8


Write C++ a program to overload the + operator for finding the sum of two Array.

#include<iostream.h>
#include<conio.h>
class array
               {
                              int a[4], i;
                              public:
                              void get ();
                              void put ();
                              array operator + (array t)
               };
void array :: get ()
{
               for (i=0; i<=3; i++)
               {
                              cin>>a[i];
               }
}
void array :: put ()
{
               for (i=0; i<=3; i++)
               {
                              Cout<<a[i]<<”\n”;
               }
}
array array :: operator + (array t)
{
               for (i=0; i<=3; i++)
               {
                              temp.a[i] = a + t.[i];
               }
               return temp;
}
void main ()
{
clrscr ();
array st, mt, kt;
st.get ();
mt.get ();
kt = st + mt;
               kt.put ();
               getch ();
}


Write C++ a program to overload the + operator for String Concatenation of two String.

#include<iostream.h>
#include<conio.h>
class String
               {
                              char *s;
                              public:
                              string ()
                              {
                              }
                              string (char *t)
                              {             
                                             len = strlen (t);
                                             S = new char [len + 1];
                                             strcpy (s, t);
                              }
                              void put ();
                              string operator + (string t);
}
void String :: put ()
{
               cout<<S;
}
String String :: operator + (string t)
{
               String temp;
               temp.len = strlen (S) + strlen (t.S);
               temp.S = new char [len + 1];
               strcpy (temp.S, S);
               strcpy (temp.S, t.S);
               return temp;
}
void main ()
{
               clrscr ();
char t[20], m[20];
               cout<<”Enter First string = ”;
               cin>>t;
               cout<<”Enter Second string = ”;
               cin>>m;
               String m1[t], m2[t], m3[t];
               m3 = m1 + m2;
               m1.put ();
               m2.put ();
               m3.put ();
               getch ();
}
 

No comments:

Post a Comment