```
// matrix2.h - A simple 3 x 3 matrix class.
// Each element is of type int.
// This version of matrix illustrates the use of global friend functions
// instead of member functions, type convertion constructors,
// and defining an inserter function for output.
//
// Written by Wayne Pollock, Tampa FL  2000.

#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>

using namespace std;

class matrix
{
enum { dim = 3 };                      // The dimensions of a matrix

public:
matrix ();                             // default constructor
matrix ( const int [dim][dim] );       // conversion constructor
matrix ( const matrix& );              // copy constructor
matrix& operator= ( const matrix& );   // assignment operator
~matrix () { /*null body*/ }           // destructor (does nothing!)

friend matrix operator+ ( const matrix&, const matrix& );
friend matrix operator* ( const matrix&, const matrix& );
friend ostream& operator<< ( ostream&, const matrix& );  // inserter

private:
int rep[dim][dim];
};

#endif
```

```
// matmain2.cpp - A file that uses the user defined type matrix.
// Written by Wayne Pollock, Tampa FL, 2000.

#include <iostream>
#include "matrix2.h"

using namespace std;

int main ()
{  const int ary1[3][3] = { 1, 1, 1,  2, 2, 2,  3, 3, 3 };
const int ary2[3][3] = { 1, 0, 0,  0, 1, 0,  0, 0, 1 };
matrix m1;                // default initialization.
const matrix m2(ary1);    // initialized from ary1.
matrix ident = ary2;      // initialized from ary2.

cout << endl << "m1 = (default):" << m1;
cout << endl << "m2 = (array of int)" << m2;
matrix m3 = m2 + m2;
cout << endl << "m3 = m2 + m2:" << m3;

m1 = ident * m3 + m2;
cout << endl << "m1 = ident * m3 + m2:" << m1;

m1 = m2 + ary1;
cout << endl << "m1 = m2 + ary1:" << m1;

m1 = ary1 + m2;
cout << endl << "m1 = ary1 + m2:" << m1;
cout << endl << "m2 * m2:" << m2 * m2;

return 0;
}

#ifdef COMMENTED_OUT	// Output of Above Program:

m1 = (default):
{
0,  0,  0,
0,  0,  0,
0,  0,  0,
}

m2 = (array of int)
{
1,  1,  1,
2,  2,  2,
3,  3,  3,
}

m3 = m2 + m2:
{
2,  2,  2,
4,  4,  4,
6,  6,  6,
}

m1 = ident * m3 + m2:
{
3,  3,  3,
6,  6,  6,
9,  9,  9,
}

m1 = m2 + ary1:
{
2,  2,  2,
4,  4,  4,
6,  6,  6,
}

m1 = ary1 + m2:
{
2,  2,  2,
4,  4,  4,
6,  6,  6,
}

m2 * m2:
{
6,  6,  6,
12, 12, 12,
18, 18, 18,
}

#endif
```

```
// matrix2.cpp - The implementation of the class matrix.
// Written by Wayne Pollock, Tampa FL, 2000.

#include <iostream>
#include <iomanip>
#include "matrix2.h"

using namespace std;

matrix::matrix ()            // Default constructor.
{  for ( int i = 0; i < dim; ++i )
for ( int j = 0; j < dim; ++j )
rep[i][j] = 0;
}

matrix& matrix::operator= ( const matrix& mat )  // overloaded assignment op.
{  for ( int i = 0; i < dim; ++i )
for ( int j = 0; j < dim; ++j )
rep[i][j] = mat.rep[i][j];
return *this;
}

matrix::matrix ( const int m[dim][dim] )    // Conversion consturctor.
{  for ( int i = 0; i < dim; ++i )
for ( int j = 0; j < dim; ++j )
rep[i][j] = m[i][j];
}

matrix::matrix ( const matrix& mat )   // Copy constructor.
{  for ( int i = 0; i < dim; ++i )
for ( int j = 0; j < dim; ++j )
rep[i][j] = mat.rep[i][j];
}

matrix operator+ ( const matrix& m1, const matrix& m2 )
{   matrix temp = m1;
for ( int i=0; i<matrix::dim; ++i )
for ( int j=0; j<matrix::dim; ++j )
temp.rep[i][j] += m2.rep[i][j];
return temp;
}

matrix operator* ( const matrix& m1, const matrix& m2 ) // cross product.
{  matrix temp;
int i, j, k, prod;
for ( i=0; i<matrix::dim; ++i )
{  for ( j=0; j<matrix::dim; ++j )
{  prod = 0;
for ( k = 0; k<matrix::dim; ++k )
prod += m1.rep[i][k]* m2.rep[k][j];
temp.rep[i][j] = prod;
}
}
return temp;
}

ostream& operator<< ( ostream &out, const matrix &m )  // Simple inserter.
{  out << endl << "  {" << endl;
for ( int i = 0; i < matrix::dim; ++i )
{  out << "     ";
for ( int j=0; j<matrix::dim; ++j )
out << setw(2) << (m.rep)[i][j] << ", ";
out << endl;
}
out << "  }" << endl;
return out;
}
```

```
# Makefile to build Matrix example C++ programs.
# Written by Wayne Pollock, Tampa Florida, 2/2000.

all: mat1.exe mat2.exe

mat1.exe: matmain1.obj matrix1.obj
bcc32 -emat1.exe matmain1.obj matrix1.obj

mat2.exe: matmain2.obj matrix2.obj
bcc32 -emat2.exe matmain2.obj matrix2.obj

clean:
del mat*.obj
del mat*.tds

cleanall: clean
del mat*.exe
```