1 / 33

Object-Oriented Programming Using C++

Object-Oriented Programming Using C++. CLASS 16. 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.

aloha
Télécharger la présentation

Object-Oriented Programming Using C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming Using C++ CLASS 16

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

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

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

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

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

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

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

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

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

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

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

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

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

  15. Stack Operations push - add a plate

  16. Stack Operations push - add a plate pop - remove a plate

  17. Stack Operations Push - add a plate pop - remove a plate is empty - there are no plates

  18. Stack Operations Push - add a plate pop - remove a plate is empty - there are no plates is full - no more room in cabinet

  19. Stack as an array of integers top -1 size 8

  20. Stack as an array of integers top 0 size 8 5 Push (5)

  21. Stack as an array of integers top 1 size 8 5 9 Push (5) Push (9)

  22. Stack as an array of integers top 2 size 8 5 9 3 Push (5) Push (9) Push (3)

  23. Stack as an array of integers top 1 size 8 5 9 Push (5) Push (9) Push (3) Pop

  24. Stack as an array of integers top 0 size 8 5 Push (5) Push (9) Push (3) Pop Pop

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

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

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

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

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

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

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

  32. Q & A

More Related