Class Template:
As you might have seen the advantage of function template is that, the code gets compact, also by writing less number of lines of code(LoC), we can achieve our desired feature from a function template.
On the other hand, a class template has a feature to let programmer create a single class that can work for family of classes. Its major use and implementation can be while handling Matrices. By writing a class in a generic way, we can save lot of memory spaces (consider matrix multiplication, for say).
The general syntax to declare a class template is :
1 2 3 4 5 6 7 8 9 |
template<class T> class User_defined_name{ private: //properties and methods protected: //properties and methods public: //properties and methods }; |
Here, ‘T’, is the parametrized data-type. We should note that, all the methods within the class template are implicitly declared as template. If the method definition is within the class itself, there is no change (normal method definition within the class).
In case , method is defined outside of the class, for template class it must follow the given general syntax:
1 2 3 4 5 6 |
template<class T> return_type User_defined_class_name<T>::method_name(T args) { //statements //return_statement } |
[Note: Class Template and Template Class can be interchangeably used, both these terms mean the same i.e generic class]
In order to create the object of the generic class, the general syntax is:
1 |
class User_defined_class_name<built_int_type> objectName; |
Now, it must have made sense to you, how is the type determined for the class-when it was declared as a generic type. For example: for template class Sample, the Object could have been declared as :
1 |
class Sample<double> objectName; |
A simple program to demonstrate the concept:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
/** Date: 2/1/2017 Author: Dhiraj Kumar Jha Email:callmedhiraj@gmail.com FB: fb.com/dhirajkumar.jha Description: This program explains the general usage class template in c++. A program is written to add two matrices and return the resultant matrix. Subtraction and Multiplication is left as learners' exercise. **/ #include<iostream> #define ROW 3 #define COL 3 using namespace std; //declaring the class template or a generic class template<class T> class MyMatrix{ private: T Mat[ROW][COL]; public: //default constructor in case of class template MyMatrix(T=0){ //initialize each of the matrix for(int i = 0; i < ROW; i++ ){ for(int j = 0; j < COL; j++){ Mat[i][j] = 0; }//inner loop ends }//outer loop ends }//default constructor ends //method to read the elements of the matrix void setElements(){ for(int i = 0; i < ROW; i++ ){ for(int j = 0; j < COL; j++){ cout<<"Enter the element at ["<<i<<"]["<<j<<"] "; cin>>Mat[i][j]; cout<<endl; }//inner loop ends }//outer loop ends }//reading the values ends ///////////////////////////////////////////////// //display the values void getElements(){ for(int i = 0; i < ROW; i++ ){ for(int j = 0; j < COL; j++){ cout<<Mat[i][j]<<"\t\t"; }//inner loop ends cout<<endl<<endl; }//outer loop ends cout<<"----------------------------------------------"<<endl; }//reading the values ends //to add two matrices, just the declaration, definition is outside of the class MyMatrix addMatrices(MyMatrix,MyMatrix); ////////////////////////////////////////////////////////////////// //method to subtract and multiplication: do by yourself };//class ends //main implementation int main(){ //create object of generic class class MyMatrix<int> iA, iB, iC; class MyMatrix<double> dA, dB, dC; //read elements for A matrix cout<<"Enter the elements of A matrix"<<endl; dA.setElements(); //read elements for B matrix cout<<"Enter the elements of B matrix"<<endl; dB.setElements(); //call the method to add these two matrices cout<<"The resultant matrix after matrix addition"<<endl; dC = dA.addMatrices(dA, dB); //display the resultant matrix dC.getElements(); ////////////////////////////////////////////// return 0; }//main ends /** @todo: Change the dA to iA, dB to iB, and dC to iC in the main() and re-compile the program or else invoke the same methods one after another for better understanding. */ //defining the template class method outside of the template class //method to sum two matrices and return the resultant matrix template<class T> MyMatrix<T> MyMatrix<T>::addMatrices(MyMatrix A, MyMatrix B){ //resultant matrix must be of the same size MyMatrix resultantMatrix; for(int i = 0; i < ROW; i++ ){ for(int j = 0; j < COL; j++){ resultantMatrix.Mat[i][j] = A.Mat[i][j] + B.Mat[i][j]; }//inner loop ends }//outer loop ends return resultantMatrix; }//addMatrices ends |
Another noticeable concept is that, if the method returns an Object of generic type, the parametrized data-type must be passed there as well. Note, the return-type, MyMatrix<T>.
Leave a Reply
You must be logged in to post a comment.