Wednesday, September 1, 2010

Operator Overloading

Operator Overloading:--

• The mechanism of giving special meaning to an operator is known as operator overloading.
• For example: C++ permits to add two variables of user defined types with the same syntax that is applied to the basic types.
• Operator Overloading provides a flexible option for the creation of new definition for most of C++ operators except
o Class member access operator (.)
o Scope Resolution Operator
o Size Operator (Size of)
o Conditional Operator (? :)
• Although the semantics of an operator can be extended, we cannot the syntax.

Defining the Operator
• Special function called operator is needed.
• Operator function must be either member function or friend function.

return_type classname::operator op (argument-list)
{
Function body;
}

Rules for Overloading Operator
• Only existing operators can be overloaded. New operators cannot be created.
• Overloaded operators follow the syntax rules of the original operators. They cannot be over ridden.
int a, b, c;
c=a+b;
rectangle r1,r2,r3;
r3=r2+r1;
• There are some operator that cannot be overloaded (., ::, ?:, sizeof).
• Unary operator overloaded by means of a member function take no explicit arguments, but those overloaded by friend function take one reference arguments.
• Binary operators overloaded by means of a member function take one explicit argument but those overloaded by friend function take two explicit argument.


Overloading Unary Operator

#include
#include

class counter{
private:
int count;
public:
counter() {
count=0;
}
void show(){
cout<
#include

class counter{
private:
int count;
public:
counter() {
count=0;
}
void show(){
cout<
#include

class counter{
private:
int count;
public:
counter() {
count=0;
}
counter (int c){
count=c;
}
void show(){
cout<
#include
class rectangle {
private:
int length;
int width;
public:
rectangle(){ rectangle (int l, int w){ length=0; length=l;
width=0; width=w;
} }
void getdata(){
cout<<”Enter length:”; cin>>length;
cout<<”Enter width:”; cin>>width;
}
void showdata(){
cout<
#include
class rectangle {
private:
int length;
int width;
public:
rectangle(){ rectangle (int l, int w){ length=0; length=l;
width=0; width=w;
} }
void getdata(){
cout<<”Enter length:”; cin>>length;
cout<<”Enter width:”; cin>>width;
}
void showdata(){
cout<

#include

enum boolen {false, true};
class length{
private:
int len;
public:
length(){
len=0;
}
length (int l){
length=l;
}
boolen operator > (length lg){
if(len>lg.length)
return true;
else
return false;
}
};
void main(){
length l1(10);
length l2(5);
if(l1>l2)
cout<<”l1 is greater”; else cout<<”l2 is greater”; } Concatenating String #include
#include
#include
const int sz=80;
class sring{
private:
char str[sz];
public:
string(){
strcpy(str,””);
}
string(char a[]){
strcpy(str,a);
}
void display(){
cout<<”the string is:”<
#include
class minus{
private:
int x, int y;
public:
minus(){
x=0;
y=0;
}
minus (int a, int b){
x=a;
y=b;
}
void show(){
cout<
#include
class length{
private:
int l;
public:
length (){
l=0;
}
minus (int a){
l=a;
}
void show(){
cout<<”length=”<
#include
class time{
private:
int hr;
int min;
public;
time (int t){
hr=t/60;
min=t%60;
}
};
void main(){
time t1;
int duration=100;
t1=duration; // int type to class type
……………..
…………….. …………….
}
• Class to basic type
o C++ allows us to define an overloaded casting operator that could be used to convert a class type data to a basic type.
o An overloaded casting operator function is usually referred to as a conversion function.
Operator typename(){
….function statement;
}
o This function converts a class type data to type name.
o The casting function must satisfy the following
 It must be a class member
 It must not specify the return type
 It must not have any arguments
Example:
#include
#include
#include
const float M=3.28;
class distance{
private:
int feet;
int inch;
public: //converting meter into feet and inches
distance (float meter){
float f=M*meter;
feet=int (f);
inch=12*(f-feet);
}
void show(){
cout<<”feet=”< source class
x -> destination class
o When the conversion routine is in the source class it is commonly implemented as a conversion function.
o When the conversion routine in destination class, it is carried out using one argument constructor. However constructor in destination class must be able to access the data in the source class to perform the conversion.

Example:
// Conversion routine in source
#include
#include
#include
class point{
pivate:
double x;
double y;
public:
point(){
x=0.0;
y=0.0;
}
point (double x1. double y1){
x=x1;
y=y1;
}
void show(){
cout<
#include
#include
class polar{
double radius;
double angle;
public:
polar( double r, double a){
radius=r;
angle=a;
}
void show(){
cout< }
double getr(){return radius; }
double geta(){return angle; }
void show(){
cout< };

class point{
pivate:
double x;
double y;
public:
point(){
x=0.0;
y=0.0;
}
point (double x1. double y1){
x=x1;
y=y1;
}
point (polar p){
double r=p.getr();
double a=p.geta();
x=r*cos(a);
y=r*sin(a);
}
void show(){
cout< }
};
void main(){
point R1;
polar P1(15.0, 0.785);
R1=point(P1);
R1.show();
P1.show();
}

No comments:

Post a Comment