Users Online
	· Guests Online: 9
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
Overload Unary Minus Operator in C++
Overload Unary Minus Operator in C++
This C++ program overloads the minus(-) operator. The program defines a class and instantiates it. The minus operator is defined inside the class. The value of two objects can be subtracted using this operator.
Here is the source code of the C++ program which overloads the minus(-) operator. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
- 
/* - 
* C++ Program to overload minus (-) operator - 
*/ - 
#include <iostream> - 
using namespace std;
 - 
 - 
class Integer {
 - 
private:
 - 
int value;
 - 
public:
 - 
Integer(int v) : value(v) { }
 - 
Integer operator-(Integer i) {
 - 
value = value - i.value;
 - 
return *this;
 - 
} - 
int getValue() {
 - 
return value;
 - 
} - 
};
 - 
 - 
int main()
 - 
{ - 
Integer a(10), b(20);
 - 
 - 
cout << "a = " << a.getValue() << "\n";
 - 
cout << "b = " << b.getValue() << "\n";
 - 
cout << "a - b = " << (a -b).getValue() << "\n";
 - 
} 
$ a.out a = 10 b = 20 a - b = -10
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.
