First we consider the unary operator. Let consider unary operator unary minus ( - ) , when it is used, it takes just one operand.
We know that unary minus (-) operator changes the sign of an operand when applied to basic data item.
We have overloaded this operator so that it should be operated to an object in the same way as applied to an int or float variables.
The unary minus when applied to an object should change the sign of each of data item.
Overloaded function can be invoked by expression such as for unary operator
Op x;
OR
x op;
#include<conio.h>
#include<iostream.h>
class space
{
int x, y, z;
public:
void getdata(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
void display( );
void operator-( );
};
void space::display( )
{
cout<<"\n\tx is--->"<<x;
cout<<"\n\ty is--->"<<y;
cout<<"\n\tz is--->"<<z;
}
void space::operator-( )
{
x = -x;
y = -y;
z = -z;
}
void main( )
{
space s;
clrscr( );
s.getdata( 10, -20, 30);
cout<<"\n\tBefore Overloading:";
s.display( );
-s;
cout<<"\n\tAfter Overloading:";
s.display( );
getch( );
}
Output:-
Before Overloading:
x is--->10
y is--->-20
z is--->30
After Overloading:
x is--->-10
y is--->20
z is--->-30
Unary Operator overloading using Friend function
We can also use unary operator with the friend function. By using this concept we can use operator function with different classes. The following program illustrates this features.
Note :
In the friend function , we are passing argument as the reference . If we pass argument as by value it will not reflect the correct result because we need all the changes outside which are made inside the operator function.
//WAP to overload unary '-' operator to negate different variables using friend function
#include<conio.h>
#include<iostream.h>
class space
{
int x, y, z;
public:
void getdata(int a, int b, int c)
{
z = a;
y = b;
z = c;
}
void display( )
{
cout<<"\n\tData--->";
cout<<"\t"<<x<<"\t"<<y<<"\t"<<z;
}
friend void operator -(space &op);
};
void operator -(space &op)
{
op.x = -op.x;
op.y = -op.y;
op.z = -op.z;
}
void main( )
{
space s;
clrscr( );
s.getdata( 10, -20, 30);
cout<<"\n\tBefore Operator overloading---";
s.display( );
-s;
cout<<"\n\tAfter Operator overloading---";
s.display( );
getch( );
}
Output:-
Before Operator overloading---
Data is---> 10 -20 30
After Operator overloading---
Data is---> -10 20 -30
We know that unary minus (-) operator changes the sign of an operand when applied to basic data item.
We have overloaded this operator so that it should be operated to an object in the same way as applied to an int or float variables.
The unary minus when applied to an object should change the sign of each of data item.
Overloaded function can be invoked by expression such as for unary operator
Op x;
OR
x op;
#include<conio.h>
#include<iostream.h>
class space
{
int x, y, z;
public:
void getdata(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
void display( );
void operator-( );
};
void space::display( )
{
cout<<"\n\tx is--->"<<x;
cout<<"\n\ty is--->"<<y;
cout<<"\n\tz is--->"<<z;
}
void space::operator-( )
{
x = -x;
y = -y;
z = -z;
}
void main( )
{
space s;
clrscr( );
s.getdata( 10, -20, 30);
cout<<"\n\tBefore Overloading:";
s.display( );
-s;
cout<<"\n\tAfter Overloading:";
s.display( );
getch( );
}
Output:-
Before Overloading:
x is--->10
y is--->-20
z is--->30
After Overloading:
x is--->-10
y is--->20
z is--->-30
Unary Operator overloading using Friend function
We can also use unary operator with the friend function. By using this concept we can use operator function with different classes. The following program illustrates this features.
Note :
In the friend function , we are passing argument as the reference . If we pass argument as by value it will not reflect the correct result because we need all the changes outside which are made inside the operator function.
//WAP to overload unary '-' operator to negate different variables using friend function
#include<conio.h>
#include<iostream.h>
class space
{
int x, y, z;
public:
void getdata(int a, int b, int c)
{
z = a;
y = b;
z = c;
}
void display( )
{
cout<<"\n\tData--->";
cout<<"\t"<<x<<"\t"<<y<<"\t"<<z;
}
friend void operator -(space &op);
};
void operator -(space &op)
{
op.x = -op.x;
op.y = -op.y;
op.z = -op.z;
}
void main( )
{
space s;
clrscr( );
s.getdata( 10, -20, 30);
cout<<"\n\tBefore Operator overloading---";
s.display( );
-s;
cout<<"\n\tAfter Operator overloading---";
s.display( );
getch( );
}
Output:-
Before Operator overloading---
Data is---> 10 -20 30
After Operator overloading---
Data is---> -10 20 -30
0 Comments:
Post a Comment