You are on page 1of 3

Program No.

Aim:- Write a Program in C++ for Unary Operater Overloading.


Program:-
#include<iostream.h>
#include<conio.h>
class space
{
int x,y,z;
public:
void get(int a,int b,int c);
void disp();
void operator-();
};
void space::get(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::disp()
{
cout<<x<<"\n"<<y<<"\n"<<z<<"\n";
}

SHRI VASANTRAO NAIK MAHAVIDHYALAYA, DHARNI Page 1


Program No. 6

void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
void main()
{
clrscr();
space s;
s.get(10,-20,30);
cout<<"Befor overloading unary minus operator:"<<endl;
s.disp();
-s;
cout<<"After overloading unary minus operator:"<<endl;
s.disp();
getch();
}

SHRI VASANTRAO NAIK MAHAVIDHYALAYA, DHARNI Page 2


Program No. 6

---------------------------------------Output-----------------------------------
Befor overloading unary minus operator:
10
-20
30
After overloading unary minus operator:
-10
20
-30

SHRI VASANTRAO NAIK MAHAVIDHYALAYA, DHARNI Page 3

You might also like