RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Example of C++ class template Array to instantiate an Array of any element type

Article Index
1. array.h
2. main.cpp
3. Compile
4. Output

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
-----------------------------

Share this linux post:

Submit Example of C++ class template Array to instantiate an Array of any element type in Delicious Submit Example of C++ class template Array to instantiate an Array of any element type in Digg Submit Example of C++ class template Array to instantiate an Array of any element type in FaceBook Submit Example of C++ class template Array to instantiate an Array of any element type in Google Bookmarks Submit Example of C++ class template Array to instantiate an Array of any element type in Stumbleupon Submit Example of C++ class template Array to instantiate an Array of any element type in Technorati Submit Example of C++ class template Array to instantiate an Array of any element type in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download