Mastering Class Templates and Function Templates in C++
This guide focuses on advanced C++ programming concepts, specifically class templates and function templates. Learn to create functions that support parameters of varying types, utilize class templates for related types, and differentiate between class templates and templated classes. The content includes practical examples such as printing arrays of different data types and summing elements using template functions. Enhance your understanding of template mechanisms in C++ for creating flexible and reusable code solutions.
Mastering Class Templates and Function Templates in C++
E N D
Presentation Transcript
Objectives • Create a function that supports parameters of a later-determined type • Use class templates to create a group of related types • Understand the difference between a class template and a templated class
Template Functions template <class T> void printArray(const T *array, const int count) { for (int i = 0; i < count; i++) cout << array[i] << “ ’; cout << endl; } Pg. 650
Template Functions template <class T> void printArray(const T *array, const int count) { for (int i = 0; i < count; i++) cout << array[i] << “ ’; cout << endl; } Pg. 650
Template Functions template <class T> void printArray(const T *array, const int count) { for (int i = 0; i < count; i++) cout << array[i] << “ ’; cout << endl; } main( ) { int a[5] = { 1, 2, 3, 4, 5}; printArray(a, 5); Pgs. 650 and 651
Template Functions template <class T> void printArray(const T *array, const int count) { for (int i = 0; i < count; i++) cout << array[i] << “ ’; cout << endl; } main( ) { int a[5] = { 1, 2, 3, 4, 5}; printArray(a, 5); Pgs. 650 and 651
Template Functions template <class T> void printArray(const T *array, const int count) { for (int i = 0; i < count; i++) cout << array[i] << “ ’; cout << endl; } main( ) { int a[5] = { 1, 2, 3, 4, 5}; printArray(a, 5); char c[6] = “HELLO”; printArray (c, 6); Pg. 651
Template Functions template <class T> void printArray(const T *array, const int count) { for (int i = 0; i < count; i++) cout << array[i] << “ ’; cout << endl; } main( ) { int a[5] = { 1, 2, 3, 4, 5}; printArray(a, 5); char c[6] = “HELLO”; printArray (c, 6); Pg. 651
Function Template Supporting 2 Types template < class T1, class T2> int sum (T1 *array, T2 value, int num-elements) { T2 sum = 0; for(int i = 0; i < num_elements; i++) sum + = array [i]; } main( ) { int values[ ] = {1, 2, 3, 4}; long int total = 0; sum(values, total, 4); cout << “total is” << total; } Pg. 652
Function Template Supporting 2 Types template < class T1, class T2> int sum (T1 *array, T2 value, int num-elements) { T2 sum = 0; for(int i = 0; i < num_elements; i++) sum + = array [i]; } main( ) { int values[ ] = {1, 2, 3, 4}; long int total = 0; sum(values, total, 4); cout << “total is” << total; } Pg. 652
Function Template Supporting 2 Types template < classT1, class T2> int sum (T1 *array, T2 value, int num-elements) { T2 sum = 0; for(int i = 0; i < num_elements; i++) sum + = array [i]; } main( ) { int values[ ] = {1, 2, 3, 4}; long int total = 0; sum(values, total, 4); cout << “total is” << total; } Pg. 652
Class Templates-Parameterized Types Pgs. 653 and 654 #ifndef TSTACK1_H #define TSTACK_H #include <iostream.h> template< class T > class Stack { public: Stack( int = 10 ); ~Stack( ) { delete [ ] stackPtr; } bool push( const T& ); bool pop( T& ); private: int size; int top; T *stackPtr; bool isEmpty( ) const { return top == -1; } bool isFull( ) const { return top == size - 1; }
Class Templates-Parameterized Types Pgs. 653 and 654 #ifndef TSTACK1_H #define TSTACK_H #include <iostream.h> template< class T > class Stack { public: Stack( int = 10 ); ~Stack( ) { delete [ ] stackPtr; } bool push( const T& ); bool pop( T& ); private: int size; int top; T *stackPtr; bool isEmpty( ) const { return top == -1; } bool isFull( ) const { return top == size - 1; } type symbol templatekeyword
Class Templates-Parameterized Types Pgs. 653 and 654 #ifndef TSTACK1_H #define TSTACK_H #include <iostream.h> template< class T > class Stack { public: Stack( int = 10 ); ~Stack( ) { delete [ ] stackPtr; } bool push( const T& ); bool pop( T& ); private: int size; int top; T *stackPtr; bool isEmpty( ) const { return top == -1; } bool isFull( ) const { return top == size - 1; } Using theType Symbol
Stack Operations push - add a plate
Stack Operations push - add a plate pop - remove a plate
Stack Operations Push - add a plate pop - remove a plate is empty - there are no plates
Stack Operations Push - add a plate pop - remove a plate is empty - there are no plates is full - no more room in cabinet
Stack as an array of integers top -1 size 8
Stack as an array of integers top 0 size 8 5 Push (5)
Stack as an array of integers top 1 size 8 5 9 Push (5) Push (9)
Stack as an array of integers top 2 size 8 5 9 3 Push (5) Push (9) Push (3)
Stack as an array of integers top 1 size 8 5 9 Push (5) Push (9) Push (3) Pop
Stack as an array of integers top 0 size 8 5 Push (5) Push (9) Push (3) Pop Pop
Templates - Constructor // Constructor with default size 10 template<class T> Stack<T>::Stack(int s) { size = s > 0 && s < 1000 ? s: 10; top = -1; stackPtr = new T[size]; } Pg. 654
Templates - Push // Push an element onto the Stack // return 1 if successful, 0 otherwise template<class T> bool Stack<T>::push(const T &pushValue) { if (!isFull( ) ) { stackPtr[++top] = pushValue; return true; } return false; } Pg. 654
Templates - Pop // Pop an element off the Stack templat<class T> bool Stack<T>::pop(T &popValue) { if (!isEmpty( ) ) { popValue = stackPtr[top- -]; return true; } return false; } #endif Pg. 654
Templates main ( ) { Stack<double>doubleStack (5); double f = 1.1; while (doubleStack.push (f)) { cout << f; f + = 1.1; } while (doubleStack.pop (f) cout << f; }
Templates Class Name main ( ) { Stack<double>doubleStack (5); double f = 1.1; while (doubleStack.push (f)) { cout << f; f + = 1.1; } while (doubleStack.pop (f) cout << f; }
Templates Class Name Object Type main ( ) { Stack<double>doubleStack (5); double f = 1.1; while (doubleStack.push (f)) { cout << f; f + = 1.1; } while (doubleStack.pop (f) cout << f; }
Templates Class Name Object Type main ( ) { Stack<double>doubleStack (5); double f = 1.1; while (doubleStack, push (f)) { cout << f; f + = 1.1; } while (doubleStack.pop (f) cout << f; } Desired Stack Size