Open In App

Overloading of function-call operator in C++

Improve
Improve
Like Article
Like
Save
Share
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:



Last Updated : 19 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads