template classes
play

Template Classes http://cs.mst.edu Syntax Rules The class - PowerPoint PPT Presentation

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


  1. Template Classes http://cs.mst.edu

  2. Syntax Rules  The class definition must be labeled as a template  Every member function must be templated http://cs.mst.edu

  3. Stacks http://cs.mst.edu

  4. Push 25 12 http://cs.mst.edu

  5. Push 32 25 25 http://cs.mst.edu

  6. Pop 32 25 25 http://cs.mst.edu

  7. Pop 25 12 http://cs.mst.edu

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  26. End of Session http://cs.mst.edu

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend