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
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check C++ Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check Computer Science Books
- Practice Computer Science MCQs