Loading....
Coupon Accepted Successfully!

 

Function Objects

A function object is a function. It is embedded in a class and hence behaves similar to an object. The class has same characteristics of a template and can be put to use with different data types. The class holds only one member function and the overloaded( )operator and never accommodates data. Function objects are frequently used as parameters for algorithms and containers.

sort ( num,num+3, greater <float>( ));

where num[] is an array. We can use an object greater<float>() to sort elements of the array in descending order. The standard template library also contains several pre-defined objects. These objects perform arithmetical and logical operations. The header file <functional>should be included while performing these operations. There are function objects that match foremost C++ operators. The variables a and b are objects of the class. K is a parameter sent to the function object. All the function objects described in Tables 20.10, 20.11, and 20.12 are defined in the functional header file.

Table 20.10  Relational function objects

Function object

Narration

not_equal­_to<K>

a != b

equal_to<K>

a = = b

greater<K>

a > b

greater_equal<K>

a >= b

less<K>

a < b

less­_equal<K>

a <= b

 

Table 20.11  Logical function objects

Function object

Narration

logical_and<K>

a && b

logical_not<K>

! a

logical­_or<K>

a | | b

 

Table 20.12  Arithmetic function objects

Function object

Narration

plus<K>

a + b

divides<K>

a / b

modulus<K>

a % b

multiplies<K>

a * b

minus<K>

a b

negate<K>

- a

 

20.18 Write a program to sort an array using function object.

#include<iostream>

#include<algorithm>

#include<functional>

using namespace std;

int main()

{

float a[]={1.5,6.5,2.5};

float b[]={6.4,4.8,1.4};

sort (a,a+3,greater<int>());

sort (b,b+3);

for (int j=0;j<3;j++) cout<<a[j]<<“”;

cout<<“\n”;

for (int k=0;k<3;k++) cout<<b[k]<<“”;

cout<<“\n”;

float s[6];

merge (a,a+3,b,b+3,s);

for (j=0;j<6;j++) cout<<s[j] <<“”;

cout<<“\n”;

return 0;

}

 

oUTPUT

6.5 2.5 1.5

1.4 4.8 6.4

1.4 4.8 6.4 6.5 2.5 1.5

 

 

Explanation: In the above program, two float arrays a[ ] and b[ ] are declared and initialized. Elements of two arrays are sorted by sort()function. The array a[ ] is sorted with the help of the function object greater<float>, and the array b[ ] is sorted without the function. At the end, the merge( ) function combines elements of both the arrays, and the resulting array is stored in the array s[ ]. The contents of the merged array are displayed.

 

20.19 Write a program to create function object.

#include<iostream>

using namespace std;

template <class S>

class show

{

public:

void operator() ( const S& s)

{

cout<<s;

}

};

int main()

{

show<int> showvalue;

for (int j=0;j<8;++j) showvalue(j);

}

 

OUTPUT

01234567

 

 

Explanation: In the above program, the operator() is overloaded in the class show. The showvalue is an object of the class show. The overloaded operator() can be used with an object of the class show type similar to the statement showvalue(j); to display the value; so, it is called a function object.





Test Your Skills Now!
Take a Quiz now
Reviewer Name