Generic Class
◀ Friend Class and Friend Function▶ Traps and Tips Amazon
The main purpose of a generic class is to provide genericity so that the same member function does not need to be rewritten to accept a different type of argument. Different programming languages support genericity in different ways. In C++, a class template is used to provide genericity for a class. The word “template” in C++ in fact is linked to genericity.
The Standard Template Library we will be covering in
Chapter 7 is a case in point. Most of you should be well familiar with a template class; if you don’t, consult the Internet or a book for its syntax.
The following is a sample program using a template to represent a two-dimensional array of various types. In
main() I create one array of
int and one array of
char and manipulate them the same way to show you how the template class
matrix is written to provide genericity.
#include <iostream>
using namespace std;
/* you can replace <class Type> with <typename Type> */
template <class Type>
class matrix{
private:
Type **array;
int rows;
int cols;
public:
matrix(int r, int c);
matrix(matrix & m); /* copy constructor */
Type* & operator[](int row) { return array[row]; }
int getRows() const { return rows; }
int getCols() const { return cols; }
void showArray();
};
template <class Type>
matrix<Type>::matrix(int r, int c) {
rows = r;
cols = c;
array = new Type*[r];
for(int i=0; i<r; i++)
array[i] = new Type[c];
}
template <class Type>
matrix<Type>::matrix(matrix & m) {
int i, j;
delete [] array;
rows = m.getRows();
cols = m.getCols();
array = new Type*[rows];
for(i=0; i<rows; i++)
array[i] = new Type[cols];
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
array[i][j] = m[i][j];
}
template <class Type>
void matrix<Type>::showArray() {
int i, j;
for(i=0; i<rows; i++) {
for(j=0; j<cols; j++)
cout << array[i][j] << '\t';
cout << endl;
}
}
int main(){
int i, j;
matrix<int> b(10,5); /* 10-by-5 array */
matrix<char> d(10,5);
for(i=0; i<10; i++)
for(j=0; j<5; j++)
b[i][j] = i+j; /* assigning values to every single cell */
cout << "Here are the contents of b:\n";
b.showArray(); /* display the array */
matrix<int> a(b); /* copy b to a */
cout << "\nHere are the contents of a:\n";
a.showArray();
for(i=0; i<10; i++)
for(j=0; j<5; j++)
d[i][j] = 'a'+i+j; /* assigning values to every single cell */
cout << "Here are the contents of d:\n";
d.showArray(); /* display the array */
matrix<char> c(d); /* copy d to c */
cout << "\nHere are the contents of c:\n";
c.showArray();
return 0;
}Go ahead and add additional functions to this class template so that it can handle row and column additions and deletions.
Pay attention to the syntax of a class template and discern the differences between a class template and a normal class.
The primary use of a class template is, obviously, to accommodate a number of different data types. In most situations, however, a class is designed specifically to work with certain data types. Therefore, don’t get intimidated by a class template; it is not as important as you may think.
◀ Friend Class and Friend Function▶ Traps and Tips