Template Classes 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 25 http://cs.mst.edu
Pop 32 25 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_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;} }; http://cs.mst.edu
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; } http://cs.mst.edu
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; } http://cs.mst.edu
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; } http://cs.mst.edu
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; } http://cs.mst.edu
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]; } http://cs.mst.edu
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]; } http://cs.mst.edu
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]; } http://cs.mst.edu
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]; } 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); [0] [1] [2] [3] [4] 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] 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] a_value ? 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); [0] [1] [2] [3] [4] a_value ? 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); [0] [1] [2] [3] [4] a_value 6 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_herdSize(0){} private: T_animal my_herd[MAX]; int m_herdSize; }; farm<elephant> large_animal_ranch; http://cs.mst.edu
End of Session http://cs.mst.edu
Recommend
More recommend