Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 7.10: Overloading Operator () for Indexing

---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

Chapter 7.10: Overloading Operator () for Indexing

Adapted from: “Object-Oriented Programming Using C++” by Ira Pohl (Addison - Wesley)

Program that demonstrates overloading the function call operator () for use in matrix indexing in C++

In file matrix3.cpp

matrix3.cpp
/*********************************************************************

  Filename:  matrix3.cpp
  Chapter:   7      Ad Hoc Polymorphism
  Section:   7.10   Overloading () for Indexing
  Compiler:  Borland C++     Version 5.0       Summer 1996
  Object Oriented Programming Using C++, Edition 2   By Ira Pohl

*********************************************************************/

//This is matrix not based on vect
//Shows () as index operator

#include  <iostream>          // Changed iostream.h to iostream
#include  <assert.h>

using namespace std;          // Added. MK.

class matrix {
public:
   matrix(int c, int r);
   ~matrix();
   double& operator()(int i, int j);
   double set_element(int i, int j, double d);
   matrix& operator=(const matrix& m);
   matrix& operator+=(matrix& m);
   void print() const;
private:
   int c_size, r_size;
   double  **p;
};

matrix:: matrix(int c, int r):c_size(c), r_size(r)
{
   p = new double*[c];
   assert(p != 0);

   for (int i = 0; i < c; ++i){
      p[i] = new double[r];
      assert(p[i] != 0);
   }
}

matrix:: ~matrix()
{
   for (int i = 0; i < c_size; ++i)
      delete [] p[i];
   delete [] p;
}

inline double& matrix::operator()(int i, int j)
{
   assert( i >= 0 && i < c_size && j >= 0 && j < r_size);
   return p[i][j];
}

matrix& matrix::operator=(const matrix& m)
{
   assert(m.c_size == c_size && m.r_size == r_size);
   int i, j;

   for (i = 0; i < c_size; ++i)
      for (j = 0; j < r_size; ++j)
         p[i][j] = m.p[i][j];
   return (*this);
}

matrix& matrix::operator+=(matrix& m)
{
   assert(m.c_size == c_size && m.r_size == r_size);
   int i, j;

   for (i = 0; i < c_size; ++i)
      for (j = 0; j < r_size; ++j)
         p[i][j] += m.p[i][j];
   return *this;
}

void matrix::print() const
{
   int i, j;
   for (i = 0; i < c_size; ++i) {
      cout << "\nrow  " << (i + 1) << endl;
      for (j = 0; j < r_size; ++j)
         cout << p[i][j] << "\t";
   }
   cout << endl;
}


void init_matrix(matrix& m, int c, int r, int start = 1)
{
   int i, j ;
   for (i = 0; i < c; ++i)
      for (j = 0; j < r; ++j)
         m(i, j) = start++;
}

int main()
{
   matrix m(3, 6) , n(3, 6);

   init_matrix(m, 3, 6, 1);
   init_matrix(n, 3, 6, 100);
   cout << "The matrix m contains ..." << endl;
   m.print();
   cout << endl;
   cout << "The matrix n contains ..." << endl;
   n.print();
   cout << endl;
   cout << "Performing n = n + m...." << endl;
   n += m;
   cout << endl;
   cout << "The matrix n contains ..." << endl;
   n.print();
}

Compilation Process

The above program is compiled and run using Gnu Compiler Collection (g++):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + \
    "Cpp_Code/Chapter_7_10_Overloading_Operator_Parenthesis_for_Indexing"
os.chdir(code_dir)
build_command = os.system("g++ matrix3.cpp -w -o matrix3")

Execution Process

exec_status = os.system("./matrix3")
The matrix m contains ...

row  1
1	2	3	4	5	6	
row  2
7	8	9	10	11	12	
row  3
13	14	15	16	17	18	

The matrix n contains ...

row  1
100	101	102	103	104	105	
row  2
106	107	108	109	110	111	
row  3
112	113	114	115	116	117	

Performing n = n + m....

The matrix n contains ...

row  1
101	103	105	107	109	111	
row  2
113	115	117	119	121	123	
row  3
125	127	129	131	133	135