Template Classes http cs mst edu Templates Rule









![Stack Example template <class T_element> class stack { private: T_element m_data[MAX]; int m_num. Elements; Stack Example template <class T_element> class stack { private: T_element m_data[MAX]; int m_num. Elements;](https://slidetodoc.com/presentation_image_h/12e6a68a5f0ebb59267b7752db8b4cb2/image-10.jpg)









![Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); [0] Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); [0]](https://slidetodoc.com/presentation_image_h/12e6a68a5f0ebb59267b7752db8b4cb2/image-20.jpg)








- Slides: 28
Template Classes http: //cs. mst. edu
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 http: //cs. mst. edu
Example. h file #ifndef SOMECLASS_H #define SOMECLASS_H // class definition template <class T> class Some. Class { ………. code here…… } #include “Some. Class. hpp” #endif . hpp file // definition of first function template <class T> void Some. Class<T>: : a_function() { ……. code here …. . } // definition of second function template <class T> int Some. Class<T>: : another_function() { ……. code here ………. } http: //cs. mst. edu
Syntax Rules § The class definition must be labeled as a template § Every member function must be templated http: //cs. mst. edu
Stacks http: //cs. mst. edu
Push 25 12 http: //cs. mst. edu
Push 32 25 http: //cs. mst. edu
Pop 32 25 http: //cs. mst. edu
Pop 25 12 http: //cs. mst. edu
Stack Example template <class T_element> class stack { private: T_element m_data[MAX]; int m_num. Elements; public: stack(): m_num. Elements(0){} stack(const stack<T_element>& source); bool push(const T_element elm); bool pop(T_element & elm); bool is. Full()const {return m_num. Elements == MAX; } bool is. Empty()const {return m_num. Elements == 0; } }; http: //cs. mst. edu
Push template <class T_element> bool stack<T_element>: : push(const T_element elm) { if (!is. Full()) { m_data[m_num. Elements] = elm; m_num. Elements++; return true; } return false; } http: //cs. mst. edu
Push // stack. hpp template <class T_element> bool stack<T_element>: : push(const T_element elm) { if (!is. Full()) { m_data[m_num. Elements] = elm; m_num. Elements++; return true; } return false; } http: //cs. mst. edu
Pop // stack. hpp template <class T_element> bool stack<T_element>: : pop(T_element & elm) { if (!is. Empty()) { elm = m_data[m_num. Elements-1]; m_num. Elements--; return true; } return false; } http: //cs. mst. edu
Pop // stack. hpp template <class T_element> bool stack<T_element>: : pop(T_element & elm) { if (!is. Empty()) { elm = m_data[m_num. Elements-1]; m_num. Elements--; return true; } return false; } http: //cs. mst. edu
Copy Constructor template <class T_element> stack<T_element>: : stack(const stack<T_element>& source) { m_num. Elements = source. m_num. Elements; for (int i=0; i < m_num. Elements; i++) m_data[i] = source. m_data[i]; } http: //cs. mst. edu
Copy Constructor template <class T_element> stack<T_element>: : stack(const stack<T_element>& source) { m_num. Elements = source. m_num. Elements; for (int i=0; i < m_num. Elements; i++) m_data[i] = source. m_data[i]; } http: //cs. mst. edu
Copy Constructor template <class T_element> stack<T_element>: : stack(const stack<T_element>& source) { m_num. Elements = source. m_num. Elements; for (int i=0; i < m_num. Elements; i++) m_data[i] = source. m_data[i]; } http: //cs. mst. edu
Copy Constructor template <class T_element> stack<T_element>: : stack(const stack<T_element>& source) { m_num. Elements = source. m_num. Elements; for (int i=0; i < m_num. Elements; i++) m_data[i] = source. m_data[i]; } http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); [0] [1] [2] [3] [4] ? ? ? http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); a_value ? [0] [1] [2] [3] [4] ? ? ? http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); a_value ? ? ? [0] [1] [2] [3] [4] ? ? ? http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); a_value ? [0] [1] [2] [3] [4] 5 ? ? http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); a_value ? [0] [1] [2] [3] [4] 5 6 ? ? ? http: //cs. mst. edu
Example stack<int> ant_hill; int a_value; ant_hill. pop(a_value); ant_hill. push(5); ant_hill. push(6); ant_hill. pop(a_value); a_value 6 [0] [1] [2] [3] [4] 5 ? ? http: //cs. mst. edu
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; http: //cs. mst. edu
Example 2 template <class T_animal> class farm { public: farm(): m_herd. Size(0){} private: T_animal my_herd[MAX]; int m_herd. Size; }; farm<elephant> large_animal_ranch; http: //cs. mst. edu
End of Session http: //cs. mst. edu