Contents[Hide]

This small C++ example program demonstrates an usage of templates within c++. In this example "template class array" can instantiate any type of arrays with single constructor and single setArray member function.

Such a behavior can also be done by overloading a constructors and setArray member function. However, in that case a programmer would need to write a member function declaration and definition for each array type separately.

This example program instantiates three different array types int, float and char with a single constructor a member function with use of typename T template.

1. array.h

#ifndef ARRAY_H_
#define ARRAY_H_

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

#include <typeinfo>

// define a clas array of type T
// the type is not know yet and will
// be defined by instantiation

// of object of class array<T> from main
template< typename T > class array {
private:
int size;
T *myarray;
public:
// constructor with user pre-defined size
array (int s) {
size = s;
myarray = new T [size];
}
// calss array member function to set element of myarray
// with type T values

void setArray ( int elem, T val) {
myarray[elem] = val;
}

// for loop to display all elements of an array
void getArray () {
for ( int j = 0; j < size; j++ ) {
// typeid will retriev a type for each value
cout << setw( 7 ) << j << setw( 13 ) << myarray[ j ]
<<
" type: " << typeid(myarray[ j ]).name() << endl;
}
cout << "-----------------------------" << endl;
}
};

#endif

2. main.cpp

#include "array.h"
int main()
{
// instantiate int_array object of class array<int> with size 2
array< int > int_array(2);
// set value to a first element
// call to array class member function to set array elements
int_array.setArray(0,3);
// set value to a second element
// NOTE: any attempt to set float to an int array will be translated to int value
int_array.setArray(1,3.4);

// call to array class member function to display array elements
int_array.getArray();

// instantiate float_array object of class array<float> with size 3
array< float > float_array(3);

// set value to a first element
// call to array class member function to set array elements
float_array.setArray(0,3.4);
// set value to a second element
float_array.setArray(1,2.8);

// call to array class member function to display array elements
float_array.getArray();

// instantiate float_array object of class array<char> with size 5
array< char > char_array(5);

// set value to a first element
// call to array class member function to set array elements
char_array.setArray(0,'H');
// set value to a other array elements
char_array.setArray(1,'E');
char_array.setArray(2,'L');
char_array.setArray(3,'L');
char_array.setArray(4,'O');

char_array.getArray();

return 0;
}

3. Compile

g++ main.cpp -o myarray

4. Output

$ ./myarray
0 3 type: i
1 3 type: i
-----------------------------
0 3.4 type: f
1 2.8 type: f
2 0 type: f
-----------------------------
0 H type: c
1 E type: c
2 L type: c
3 L type: c
4 O type: c
-----------------------------


Free Linux eBooks

Go to top