Home »
C++ programs »
C++ operator overloading programs
C++ program for unary minus (-) operator overloading
This program will demonstrate the example of Unary Minus (-) Operator Overloading program in C++ programming language.
Unary minus (-) operator overloading program in C++
/*C++ program for unary minus (-) operator overloading.*/
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n=x;
}
//function to display number
void dispNum(void)
{
cout << "value of n is: " << n;
}
//unary - operator overloading
void operator - (void)
{
n=-n;
}
};
int main()
{
NUM num;
num.getNum(10);
-num;
num.dispNum();
cout << endl;
return 0;
}
Output
value of n is: -10