Template Classes
Templates Rule Like function templates, class template code must be placed above main (for a single file program) or in a header file (for multiple file programs). The function definitions for the class will either reside in the same header file, or they can be placed in a .hpp file (also non-compilable) with a #include command appropriately placed in the .h file
Example .h file #ifndef SOMECLASS_H #define SOMECLASS_H // class definition template <class T> class SomeClass { ……….code here…… } #include “SomeClass.hpp” #endif .hpp file // definition of first function template <class T> void SomeClass<T>::a_function() { ……. code here ….. } // definition of second function int SomeClass<T>::another_function() ……. code here ……….
Syntax Rules The class definition must be labeled as a template Every member function must be templated
Stacks
Push 25 12
Push 32 25 25
Pop 32 25 25
Pop 25 12
Stack Example template <class T_element> class stack { private: T_element m_data[MAX]; int m_numElements; public: stack():m_numElements(0){} stack(const stack<T_element>& source); bool push(const T_element elm); bool pop(T_element & elm); bool isFull()const {return m_numElements == MAX;} bool isEmpty()const {return m_numElements == 0;} };
Push template <class T_element> bool stack<T_element>::push(const T_element elm) { if (!isFull()) m_data[m_numElements] = elm; m_numElements++; return true; } return false;
Push // stack.hpp template <class T_element> bool stack<T_element>::push(const T_element elm) { if (!isFull()) m_data[m_numElements] = elm; m_numElements++; return true; } return false;
Pop // stack.hpp template <class T_element> bool stack<T_element>::pop(T_element & elm) { if (!isEmpty()) elm = m_data[m_numElements-1]; m_numElements--; return true; } return false;
Pop // stack.hpp template <class T_element> bool stack<T_element>::pop(T_element & elm) { if (!isEmpty()) elm = m_data[m_numElements-1]; m_numElements--; return true; } return false;
Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) { m_numElements = source.m_numElements; for (int i=0; i < m_numElements; i++) m_data[i] = source.m_data[i]; }
Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) { m_numElements = source.m_numElements; for (int i=0; i < m_numElements; i++) m_data[i] = source.m_data[i]; }
Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) { m_numElements = source.m_numElements; for (int i=0; i < m_numElements; i++) m_data[i] = source.m_data[i]; }
Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) { m_numElements = source.m_numElements; for (int i=0; i < m_numElements; i++) m_data[i] = source.m_data[i]; }
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6);
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] ? ? ? ? ?
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ? ? ? ? ? ?
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ??? ? ? ? ? ?
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ? 5 ? ? ? ?
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ? 5 6 ? ? ?
Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value 6 5 ? ? ? ?
Example 2 class elephant { private: float m_wt; string m_name; public: elephant(string name, float wt); void do_something(); }; stack<elephant> pack_o_derms;
Example 2 template <class T_animal> class farm { public: farm():m_herdSize(0){} private: T_animal my_herd[MAX]; int m_herdSize; }; farm<elephant> large_animal_ranch;
End of Session