This C++ program illustrates binary function object base. The Compare inherits from the binary_function object base defined in functional library. The numbers taken from input are passed to the function object and a boolean result is returned. If the result is true, the numbers are equal otherwise they are unequal.
Here is the source code of the C++ program illustrates unary function object base. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to illustrate binary function objects bases
*/
#include <iostream>
#include <functional>
#include <algorithm>
struct Compare : public std::binary_function<int, int, bool> {
bool operator() (int a, int b)
{
return (a == b);
}
};
int main()
{
Compare compare;
Compare::first_argument_type a;
Compare::first_argument_type b;
Compare::result_type r;
std::cout << "Enter a = ";
std::cin >> a;
std::cout << "Enter b = ";
std::cin >> b;
r = compare(a, b);
std::cout << "Numbers " << a << " and " << b;
if(r == true)
std::cout << " are equal.\n";
else
std::cout << " are unequal.\n";
}
$ a.out Enter a = 10 Enter b = 20 Numbers 10 and 20 are unequal. $ a.out Enter a = 10 Enter b = 10 Numbers 10 and 10 are equal.
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:
- Practice Computer Science MCQs
- Check Programming Books
- Check Computer Science Books
- Check C++ Books
- Practice Programming MCQs