Program of Overloading (-) minus unary operator in C++.

 

 Operator Overloading : Overloading an operator simply means attaching additional meaning and semantics to an operator.In case of unary operator overloaded using a member function no argument is passed to the function whereas in case of friend function a single argument must be passed.

 

 

Following program overloads the unary operator to negate an object.

 

Code for Program that implements overloaded ” — ” minus unary operator in C++ programming.

 

Let’s open turbo C/C++ and write the following Code :

 

Code:

 

#include<iostream.h>

#include<conio.h>

class integer

{

private:

long int x,y,z,a,b,c;

public:

void getdeta();

void display();

void operator – ();

};

void integer::getdeta()

{

cout<<”\n enter value of x: \n ”;

cin>>a;

cout<<”enter value of y :\n”;

cin>>b;

cout<<”enter value of z:\n”;

cin>>c;

}

void integer::display()

{

cout<<”\n New value is:\n”;

cout<<”value of x : ”<<x;

cout<<”\n”;

cout<<”vlaue of y : ”<<y;

cout<<”\n”;

cout<<”value of z : ”<<z;

}

void integer::operator – ()

{

x=-a;

y=-b;

z=-c;

}

void main()

{

clrscr();

integer s;

s.getdeta();

-s;

s.display();

getch();

}

 

 

 

Output:

 

 

output

 

 

Happy Coding.

 

 

Tags: , , ,

Comments are closed.