Let’s start our first example by making a new C++ file in the shell. The very well-known command “touch” will be used for this purpose. After making a C++ file, you need an editor to open it. The “Gnu Nano” editor is the most recommended one. Thus, we have been using the “nano” editor to open it and create a C++ code in it.
After the file is opened, start your code by including the “input-output” stream header at the top of it. The standard namespace has been given; The class “Func” has been started. The operator() has been implemented within this class, which looks like a function, but it is a call to functor operator(). This operator looks like it is taking two arguments and returning true or false if “x” is greater than “y” to the main method. The main method has defined three integer type variables, with integer values initialized to both “x” and “y”.
Another integer variable, “res” is declared to get the return value from the functor. This “f(x,y)” seems like a function call, but it is not. This syntax is used here to call the operator() of a functor. If the variable “x” is greater than”y”, it will return 1; otherwise, 0 to the variable “res”. The “cout” statement has been used here to print out the resultant value.
After saving the code with “Ctrl+S”, leave it using the “Ctrl+X” shortcut. Now, it’s time to compile the c++ code. For compilation, make sure you have the g++ compiler already configured on your Ubuntu 20.04 system. So, the g++ command is utilized here to compile the “functor.cc” file. After this, the “./a.out” has been used to execute the file. The execution displays “1” in return. This means that variable “x” is greater than variable “y”.
Let’s make an update to our C++ code. Open the same file using the nano editor on the shell as below. The only change you have to do is replace the “greater than” sign with the “less than” sign in the operator(). The remaining code will be left unchanged. Save the updated file and leave it while using the “Ctrl+S” and “Ctrl+X” consecutively.
After saving the file, the code must be compiled once again. So, the g++ compiler command is used once again to do so. After the compilation, the execution of this newly updated file returns 0 as the variable “x” is no less than variable “y” i.e., 13 and 6.
Let’s say you want to add up two integers without using some function. The functor is here for your help. It can use the plus operator within it to do so. So, open the C++ file “functor.cc” in the editor, i.e., GNU Nano, to update its code once again. The “nano” command will be utilized for this purpose. After opening this file, let’s update at the same location where we have done before. We have replaced the “less than” sign with the “plus” sign between the variables. This is to add up two variable values and return this value to the object call.
The function call will save this value to another variable, “res” and display it on the shell with the help of a “cout” clause. The same code will be applied to do subtraction with the “-“ sign. Again save your code by pressing “Ctrl+S”. Now, return to the shell terminal with the use of “Ctrl+X”.
After quitting a C++ file, compile the newly updated code in it once more using the g++ compiler along with the name of a C++ file, i.e., functor.cc as below. After the compilation, run the updated code file with the help of a “./a.out” instruction as per the below image. The output shows the value 19 due to the summation of two integer type values, i.e., 13 and 6.
Let’s have a new example to illustrate the functioning of a functor object. Create a new C++ file or use the old file, i.e., functor.cc, with the help of a “touch” instruction. Open the created file within the GNU Nano editor using the “nano” instruction, as shown in the image attached at the start of this article. Now, add the header file of an input-output stream at the topmost of the file. The standard namespace must be declared using the keyword “using”. The Class “Func” has been declared, and it contains public-type objects in it. The first one is its constructor named “Func” taking an integer type argument in its parameter. This integer type argument value will be saved to the variable “a” with the help of “_a” and “(a)”. This constructor doesn’t do a lot more than that.
After this, the operator of a functor is used here to subtract two integer type values. The class “Func” has the private data type variable “_a” defined in it. Here comes the main method. First of all, we have created an object “f1” of a class “Func” and passed it an integer type value, i.e., “13”. Now, right after creating this object “f1”, the constructor function “Func” of a class “Func” will be executed and save the value 13 to an integer variable “_a”. After this, the “cout” statement has been used here to display the object of a class. There is nothing to be confused about.
When the object is called, it means the functor operator() is being called here and assigned a value “6”. Through this, the functor uses operator() to calculate the subtraction of two variables and return it to the main program. Let’s save the code file and quit it after that. Make use of “Ctrl+S” and “Ctrl+X” here.
Now the code of the C++ functor.cc file has been saved; compile it first before execution. Use the same “g++” compiler command to make it error-free. The compilation got successful, and we have moved to execution. While executing this file with the “./a.out” command, we got 7 as a subtraction result.
So, this was all about using Functors in C++ while working on the Ubuntu 20.04 system. We have made use of operator() to call the functor. We have also seen how to make use of Class and its object to use Functor in C++. We believe all the examples are easy to do and help you understand the concept of Functors easily while working in the Ubuntu 20.04 Linux System.