Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template Classes.

Similar presentations


Presentation on theme: "Template Classes."— Presentation transcript:

1 Template Classes

2 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

3 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 ……….

4 Syntax Rules The class definition must be labeled as a template
Every member function must be templated

5 Stacks

6 Push 25 12

7 Push 32 25 25

8 Pop 32 25 25

9 Pop 25 12

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

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

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

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

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

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

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

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

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

19 Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value);
ant_hill.push(5); ant_hill.push(6);

20 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] ? ? ? ? ?

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

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

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

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

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

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

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

28 End of Session


Download ppt "Template Classes."

Similar presentations


Ads by Google