Overloading of function-call operator in C++ Last Updated : 19 Apr, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by “()” which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overloaded, an operator function is created that can be used to pass parameters.It modifies that how the operator is fetched by the object.In Object-Oriented Languages, operator() can be treated as a normal operator, and objects of a class type can call function (named operator()) like making a function call to any other overloaded operator.When the function call operator is overloaded, a new way to call a function is not created rather an operator() function is created that can be passed an arbitrary number of parameters. Program: Below is the program of taking input in a matrix using friend functions first to overload insertion operator and extraction operator and then overloading operator() for taking input for the ith row and the jth column of the matrix and displaying value at the ith row and the jth column stored in the matrix: C++ // C++ program to illustrate the // above concepts #include <bits/stdc++.h> #include <iostream> using namespace std; #define N 3 #define M 3 // Matrix Class class Matrix { private: int arr[N][M]; public: // Overloading of input stream friend istream& operator>>( istream&, Matrix&); // Overloading of output stream friend ostream& operator<<( ostream&, Matrix&); int& operator()(int, int); }; // Function to overload the input // ">>" operator istream& operator>>(istream& cin, Matrix& m) { int x; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { // Overloading operator() // to take input cin >> m(i, j); } } return cin; } // Function to overload the output // "<<" operator ostream& operator<<(ostream& cout, Matrix& m) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { // Overloading operator() to // take input m.operator()(i, j); cout << m(i, j) << " "; } cout << endl; } return cout; } // Function to call the operator // function to overload the operators int& Matrix::operator()(int i, int j) { return arr[i][j]; } // Driver Code int main() { Matrix m; printf("Input the matrix:\n"); // Compiler calls operator >> and // passes object cin and object m // as parameter operator>>(cin, m); cin >> m; printf("The matrix is:\n"); // Compiler calls operator << and // passes object cout and object m // as parameter operator<<(cin, m); cout << m; return 0; } Output: Comment More infoAdvertise with us Next Article Overloading of function-call operator in C++ K kingluffy17 Follow Improve Article Tags : C++ Programs C++ cpp-operator cpp-operator-overloading C++-Operator Overloading +1 More Practice Tags : CPPcpp-operator Similar Reads How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read Overloading function templates in C++ Template: A template is a tool that reduces the efforts in writing the same code as templates can be used at those places.A template function can be overloaded either by a non-template function or using an ordinary function template. Function Overloading: In function overloading, the function may ha 3 min read Overloading the Comma Operator In C++, we can overload the comma operator using Operator Overloading. For Example: For "Send the query X to the server Y and put the result in variable Z", the "and" plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expr 5 min read How to call function within function in C or C++ When we begin programming in C/C++, we generally write one main() function and write all our logic inside this. This approach is fine for very small programs, but as the program size grows, this become unmanageable. So we use functions. We write code in the form of functions. The main function alway 4 min read Overloading Relational Operators in C++ In C++, operator overloading is used to redefine the behavior of already existing operators. Similarly, overloading the relational operators is commonly used to compare the instances of user-defined classes. By overloading these operators we can easily define the behavior of comparisons for the obje 4 min read Passing a Function as a Parameter in C++ Passing a function as an argument is useful in dynamically changing the behaviour of the function. This concept has already been used while passing a custom comparator function as an argument in sort() function to sort a sequence of objects as per the need.Function that is passed as argument is call 4 min read Function Overloading vs Function Templates in C++ In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, 4 min read How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo 2 min read How to Overload the Multiplication Operator in C++? In C++, the multiplication operator is a binary operator that is used to find the product of two numeric values. In this article, we are going to learn how to overload the multiplication operator for a class in C++. Overloading Multiplication Operator in C++C++ provides the functionality of operator 2 min read How to Use Default Arguments in Function Overloading in C++? In C++, we can provide the default values for the input arguments into the functions and it is also supported in function overloading. In this article, we will learn how to use default arguments in function overloading in C++. Default Arguments in Function Overloading in C++We can define the default 2 min read Like