Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CSIT

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Unit-4: Operator Overloading:

Fundamental of operator overloading:


Operator overloading is an important concept in C++. It is a type of polymorphism in which
an operator is overloaded to give user defined meaning to it. Overloaded operator is used
to perform operation on user-defined data type. For example '+' operator can be
overloaded to perform addition on various data types, like for Integer, String
(concatenation) etc.
Restriction on operator overloading:
I. No new operators can be created, only existing operators can be overloaded.
II. Numbers of Operands cannot be changed. Unary operator remains unary, binary
remains binary etc.
III. Cannot redefine the meaning of a procedure. You cannot change how integers are
added.
IV. Almost any operator can be overloaded in C++. However there are few operator
which cannot be overloaded. Operator that are not overloaded are follows:
 scope operator - ::
 sizeof
 member selector - .
 member pointer selector - *
 ternary operator - ?:
To overload operator ,a operator function IS defined inside a class.

Unary and Binary operator overloading:


Operator keyword:
The keyword operator defines a new action or operation to the operator.

Operator Argument:
An overloaded operator has a return type and a parameter list.
Box operator+(const Box&);
declares the addition operator that can be used to add two Box objects and returns final
Box object. Most overloaded operators may be defined as ordinary non-member functions
or as class member functions. In case we define above function as non-member function
of a class then we would have to pass two arguments for each operand as follows −
Box operator+(const Box&, const Box&);
1. Unary operator overloading:
The unary operators operate on a single operand and following are the examples of
Unary operators:-
The increment (++) and decrement (--) operators.
The unary minus (-) operator.
The logical not (!) operator.
//program to show eg of prefix unary operator overloading:
#include <iostream>
using namespace std;
class Rectangle
{
private:
int l,b;
public:
Rectangle()
{
l=0;
b=0;
}
void operator ++()
{
++l;
++ b;
}
void Display()
{
cout<<"Length: "<<l<<endl;
cout<<"breadth"<<b;
}
};

int main()
{
Rectangle R;
cout<<"value of length and breadth before increment:"<<endl;
R.Display();
// this calls "function void operator ++()" function
cout<<endl<<"value of length and breadth After increment:"<<endl;
++R; //R.operator++()
//cout<<"value of count After increment:"<<endl;
R.Display();
return 0;
}
Output:

//program to show eg of postfix unary operator overloading:


#include <iostream>
using namespace std;
class Rectangle
{
private:
int l,b;
public:
Rectangle()
{
l=0;
b=0;
}

void operator ++()


{
l++;
b++;
}
void Display()
{
cout<<"Length: "<<l<<endl;
cout<<"breadth"<<b;
}
};

int main()
{
Rectangle R;
cout<<"value of length and breadth before increment:"<<endl;
R.Display();
// this calls "function void operator ++()" function
cout<<endl<<"value of length and breadth After increment:"<<endl;
R++// R.operator++().
//cout<<"value of count After increment:"<<endl;
R.Display();
return 0;
}

1. Binary operator overloading:


The binary operators take two arguments and following are the examples of Binary
operators. We use binary operators very frequently like addition (+) operator,
subtraction (-) operator and division (/) operator.
//program to show example of Arithmetic binary operator overloading.
#include<iostream>
using namespace std;
class Rectangle
{
int L,B;
public:
Rectangle() //Default Constructor
{
L = 0;
B = 0;
}
Rectangle(int x,int y) //Parameterized Constructor
{
L = x;
B = y;
}

Rectangle operator+(Rectangle Rec) //Binary operator overloading func.


{
Rectangle Robj;

Robj.L = L + Rec.L;//L for R1 and Rec.L for R2(passing as argument)


Robj.B = B + Rec.B;
return Robj;
}
void Display()
{
cout<<"Length : "<<L<<endl;
cout<<"Breadth : "<<B<<endl;
}
};
int main()
{

Rectangle R1(2,5),R2(3,4),R3;
//Creating Objects
cout<<endl<<"Rectangle 1 : ";
R1.Display();
cout<<endl<<"Rectangle 2 : ";
R2.Display();
R3 = R1 + R2; //R3=R1.operator+(R2)
cout<<endl<<"Rectangle 3 : ";
R3.Display();
return 0;
}
Note: In statement 1, Left object R1 will invoke operator+() function and right object R2 is
passing as argument.
Output:

Comparison operator overloading:


// program to show example of Comparison operator overloading.
#include<iostream>
using namespace std;
class Time
{
int hour;
int min;
public:
void getdata()
{
cout<<"enter hour and minute"<<endl;
cin>>hour>>min;
}
int operator<(Time t)
{
int ft,sd;
ft=hour*60+min;//convert into minute
sd=t.hour*60+t.min;
if(ft<sd)
return 1;
else
return 0;
}
};
int main()
{
Time t1,t2;
t1.getdata();
t2.getdata();
if(t1<t2)
cout<<"t1 is less than t2"<<endl;
else
cout<<"t1 is greater or equal to t2"<<endl;
return 0;
}

Output:

Assignment operator overloading:


// program to show example of Assignment operator overloading.
#include<iostream>
using namespace std;
class Marks
{
private:
int m1;
int m2;
public:
//Default constructor
Marks()
{
m1 = 0;
m2 = 0;
}
// Parametrised constructor
Marks(int i, int j)
{
m1 = i;
m2 = j;
}
// Overloading of Assignment Operator
void operator=(const Marks &M )
{
m1 = M.m1;
m2 = M.m2;
}
void Display()
{
cout << "Marks in 1st Subject:" << m1<<endl;
cout << "Marks in 2nd Subject:" << m2<<endl;
}
};
int main()
{
// Make two objects of class Marks
Marks Mark1(45, 89);
Marks Mark2(36, 59);
cout << " Marks of first student : "<<endl;
Mark1.Display();
cout << " Marks of Second student :"<<endl;
Mark2.Display();
// use assignment operator
Mark1 = Mark2;
cout << " new assigned Marks of first Student :"<<endl;
Mark1.Display();
return 0;
}

Output:
2. //Another example of Assignment operator overloading
#include<iostream>
using namespace std;
class myclass
{
int x;
int y;
public:
myclass()
{

}
myclass( int x1,int y1)
{
x=x1;
y=y1;

}
void operator=(myclass &m)
{
x=m.x;
y=m.y;
}
void show()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
};
int main()
{
myclass j(10,20);
myclass i;
i=j; //i.operator=(j)
j.show();
i.show();
}
output:
Data conversion:
Those data types which are available as keywords are called primitives data types. for
eg.int,float,char,double etc.already defined to compiler.
user defined is any class type we defined.

Why conversion need?


int x=4;
float y;
y=x;//automatic type conversion

float y=3.14;
int x;
x=y;//automatic type conversion

sample s;
int x=5;
s=x;//error
so that there is required type conversion.

Data conversion in C++ includes conversions between basic types and user-defined
types, and conversions between different user-defined types.
The assignments between types, whether they are basic types or user-defined types, are
handled by the compiler with no effort on our part, provided that the same data type is
used on both sides of the equal sign.

The different possible data conversion are:


1. Conversion from basic to basic data type:
-implicit conversion:
-explicit conversion:
(Already done in unit 2)
2. Conversion from basic to user defined data type
3. Conversion from user-defined data type to basic data type
4. User defined to another user defined data type conversion:
 Routine in the destination class
 Routine in the source class

a. Conversion from basic to user defined data type:


Conversion from basic to user defined type is done by using the constructor function
with one argument of basic type as follows.
//program to show data conversion from basic to user defined data type
#include<iostream>
using namespace std;
class sample
{
private:
int a;
public:
sample( int x)
{
a=x;
}
void disp()
{
cout<<"the value of a="<<a<<endl;
}
};
int main()
{
int m=10;
sample s=m;//running as s.sample(m)
//s=m;
s.disp();
return 0;
}Output:

b) Conversion from user-defined data type to basic data type:


Conversion from user-defined type of basic data type is done by overloading the cast
operator of basic type as a member function. Operator function is defined as an
overloaded basic data type which takes no arguments. Return type of operator is not
specified because the cast operator function itself specifies the return type.
//program to show data conversion from user defined to basic data type
#include<iostream>
using namespace std;
class sample
{
private:
float a;
public:
sample( )
{
a=10.23;
}
operator int ()
{
int x;
x=a;
return x;
}

};
int main()
{
sample s;
int y=s;//s.operator int() calling casting operator
cout<<"the value of y="<<y<<endl;
return 0;
}
Output:

User defined to another user defined data type conversion:


Different class types:
class alpha
{
int a;
----
----
};
class beta
{
int b;
----
----
};
void main()
{
alpha obja(10);
beta objb;
objb=obja;//error because these are different category one is alpha and another is beta
category.
}
We can do this by two methods:
1. Through constructor
if objb=obja;
here constructor will run for left side (objb) so we should create constructor in beta class.

2. Through a casting operator.


if objb=obja;
here operator casting will run for right side (obja) so we should create operator casting in
alpha class.

 Routine in the source class:


If we want to convert an object of one class to an object of another class, it is
necessary that the operator function be placed in the source class.
//program to show example of Routine in the source class
#include<iostream>
using namespace std;
class cm
{
int a;
public:
cm()
{

}
cm(int n)
{
a=n;
}
void show()
{
cout<<"equivalent cm:"<<a<<endl;
}
};
class meter
{
float x;
public:
void getdata()
{
cout<<"Input value in meter"<<endl;
cin>>x;
}
operator cm()
{
return cm(x*100);
}
};
int main()
{
meter m;
m.getdata();
cm c;
c=m;
c.show();
}
Output:

 Routine in the destination class:


//program for conversion routine in destination
#include<iostream>
using namespace std;
class alpha
{
int a;
public:
alpha()
{

}
alpha(int x)
{
a=x;
}
int getvalue()
{
return a;
}
};
class beta
{
int b;
public:
beta()
{
}
beta(int x)
{
b=x;
}
beta (alpha temp)
{
b=temp.getvalue();
cout<<"constructor method"<<endl;
cout<<"b="<<b<<endl;
}
};
int main()
{
alpha obja(10);
beta objb=obja;
return 0;
}

Output:

You might also like