# C++ Templates --A Simple Example

October 16, 2017 | Autor: Mark Wilson | Categoria: Computer Science, Programming Languages

#### Descrição do Produto

because we are dealing with square matrices(whose sizes are fixed once the variables ROWS and COLUMNS are initialized), so the number of columns of first matrix will always be equal to the number of rows of the second matrix. In order to understand how the * operator works, let us consider a simple example a=

1 2 3 4

b=

5 6 7 8

19 22 43 50 Now, both a and b are of dimensions 2 by 2. Therefore, their product will be of dimension 2 by 2. The first element of a*b is gotten by summing the product of the corresponding elements of the first row of matrix a and first column of matrix b i.e. (a*b)11 = 1*5 + 2*7 = 5 + 14 = 19. (a*b)ij denotes the element at the intersection of ith row and jth column of the product matrix a*b. Similarly, the (a*b)12 is gotten by summing the product of the corresponding elements of the first row of matrix a and second column of matrix b. Similarly the other two elements can be obtained. Now, a*b =

The ~ operator transposes the elements of a matrix. By transpose, we mean interchanging the rows and columns of a matrix. So, a matrix (A)ij after the transpose operation becomes (A)ji. For example the matrix

1 2 after transposing becomes 3 4

1 3 . 2 4 The above examples may seem to be trivial, but they were purposefully made trivial in order to understand the concepts of basic matrix operations. If the matrices were of large dimensions, it wouldn' t be a trivial task to multiply them manually. In the results section, operations are performed on two matrices each of dimension 4 by 4. It is here that operator overloading proves to be most useful. For example, if we have to find the transpose of a+b*c, all we need to do is ~(a+b*c) and store this in a matrix and print the resultant matrix using the printm() function.

Also note that each operator accepts a constant reference to the matrix, this is because we want each operator to perform its function without modifying the original matrix which was given to it. Rajanikanth Jammalamadaka opmatrix.h // C++ header file #include #include using std::cin; using std::cout; using std::vector; using std::endl; const int ROWS = 2; const int COLS = 2; template class matrix { //declare a vector of vectors of type T vector< vector > s ; public: //Initialize the size of s to ROWS by COLS matrix(): s(ROWS, vector(COLS)) {} void readm(); void printm(); //declare the operators +,-,*,~ as friends and with return type matrix friend matrix operator+(const matrix&, const matrix&); friend matrix operator-(const matrix&, const matrix&); friend matrix operator*(const matrix&, const matrix&); friend matrix operator~(const matrix&); };

template void matrix::readm() { for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLS; j++) cin >> this->s[i][j];

}

}

template void matrix::printm() { for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) couts[i][j]