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

template classes
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

http://cs.mst.edu

Template Classes

slide-2
SLIDE 2

http://cs.mst.edu

Syntax Rules

  • The class definition must be labeled as a template
  • Every member function must be templated
slide-3
SLIDE 3

http://cs.mst.edu

Stacks

slide-4
SLIDE 4

http://cs.mst.edu

Push 12 25

slide-5
SLIDE 5

http://cs.mst.edu

Push 25 25 32

slide-6
SLIDE 6

http://cs.mst.edu

Pop 25 25 32

slide-7
SLIDE 7

http://cs.mst.edu

Pop 12 25

slide-8
SLIDE 8

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;} };

slide-9
SLIDE 9

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; }

slide-10
SLIDE 10

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; }

slide-11
SLIDE 11

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; }

slide-12
SLIDE 12

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; }

slide-13
SLIDE 13

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]; }

slide-14
SLIDE 14

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]; }

slide-15
SLIDE 15

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]; }

slide-16
SLIDE 16

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]; }

slide-17
SLIDE 17

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);

slide-18
SLIDE 18

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]

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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); 5 ? ? ? ? [0] [1] [2] [3] [4] ? a_value

slide-22
SLIDE 22

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); 5 6 ? ? ? [0] [1] [2] [3] [4] ? a_value

slide-23
SLIDE 23

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); 5 ? ? ? ? [0] [1] [2] [3] [4] 6 a_value

slide-24
SLIDE 24

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;

slide-25
SLIDE 25

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;

slide-26
SLIDE 26

http://cs.mst.edu

End of Session