Open In App

Overloading of function-call operator in C++

In this article, we will discuss the Overloading of the function-call operators in C++.

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++ 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:




Article Tags :