Thursday 5 April 2018

Overloading Unary Operator

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

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses