Skip to main content

Template Class


Template class

In this part you will learn:
1. What is template class
2. Why we need template class
3. Syntax
4. Program
5. Output

Template Class
Just like template functions, we can declare a class template too. For those who don’t know what template function is, it is functionality in c++ in which we generalize the data type so that it can be used for any data type as per the values using the template function.
In case of classes, it can differ by the type(int, char, double etc) of object we are making for a class, then all the member function will start treating the data members as that type. It allows classes to have members that use template parameters as types. Many programming tasks, we require a class which can word for all data types, So instead of defining every data type separately, we generalize the class using Template class functionality.
Syntax
Following is the syntax of defining a member function outside the class.
template
class Class_Name
{
any_name variable_names;
}
template
void Class_name :: function_name(parameters)
{
//definiton
}
In the above syntax, the most important thing to note is that while defining a function outside the class, first we have to define that it is a template thing, then while referring to the class we have to type the name of variable we are generalizing the class with.
Program with Template Class
The following example illustrates the concept of Template Class by the usage of Sets.
Basic Step:

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These two are the most common and basic lines of a c++ program. The first one iostream is used to grant us most of the basic functions of C++ like Input and Output functions. The second one conio.h means console input output, the black screen we see is the console. Using namespace std; provides us an ease of using all the library functions without specifying that this function belongs to standard library. Without including it, we have to write ‘std’ with all the built-in functions.
Class
  1. template <typename T>
  2. class set
  3. {
  4. public:
  5. T *ele;
  6. int size;
  7.  
  8. set(){
  9. size = 0;
  10. ele = NULL;
  11. }
  12.  
  13. set(int n){
  14. size = n;
  15. ele = new T[size];
  16. }
  17.  
  18. ~set(){
  19. if (size > 0)
  20. delete[]ele;
  21. ele = NULL;
  22. }
  23.  
  24. void fill_set();
  25. void print_set();
  26. };
  27.  
  28. template <typename T>
  29. void set<T>::fill_set()
  30. {
  31. cout << endl;
  32. for (int i = 0; i < size; i++)
  33. {
  34. cout << "Enter element: ";
  35. cin >> ele[i];
  36. }
  37. }
  38.  
  39. template <typename T>
  40. void set<T>::print_set()
  41. {
  42. cout << endl << "The set is: {";
  43. for (int i = 0; i < size; i++)
  44. {
  45. cout << ele[i];
  46. cout << ",";
  47. }
  48. cout << "}";
  49. }

In the above class which is a template class, I have declared the member variable which is a pointer of type T, so that whatever type the user gives while initializing an object, the member variable ele gets that type. Furthermore I have defined the default and overloaded constructor as well as destructor in the class and the functions fill_set and print_set outside the class so you get an idea how to define them in both ways.
One more important thing here is while dynamically allocating the memory in the overloaded constructor, I have written “ele=new T[size]” the reason behind this is that the memory going to be allocated dynamically will be depended on the type of object we are initializing. i.e. char and int have different types of memory allocation so we have generalized it with T.
Let us now take a look at the main (driver) function of the program.
  1. int main()
  2. {
  3. int size;
  4.  
  5. cout << "Enter number of elements of the sets: ";
  6. cin >> size;
  7.  
  8. set<int> set1(size);
  9. set<char> set2(size);
  10.  
  11. set1.fill_set();
  12. set2.fill_set();
  13.  
  14. set1.print_set();
  15. set2.print_set();
  16.  
  17. cout << endl;
  18.  
  19. system("pause");
  20. }

In the main function first user is being asked about the size of the sets, later two objects are being initialized of size “size”. The key point here is that “set set1(size);” means that we are initializing a class with int as the type of the elements we want to store in the set. It can be any other data type like float, double, char etc.
Then the functions of fill_set and print_set are being called to store and print the elements of set on the screen respectively.

Output
picture viewer

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.