+ All Categories
Home > Documents > Object-Oriented Programming Using C++

Object-Oriented Programming Using C++

Date post: 05-Jan-2016
Category:
Upload: aloha
View: 28 times
Download: 9 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
33
1 IDLOOPC1998. Object-Oriented Programming Using C++ CLASS 16
Transcript
Page 1: Object-Oriented  Programming  Using C++

1IDLOOPC1998.

Object-Oriented Programming

Using C++

CLASS 16

Page 2: Object-Oriented  Programming  Using C++

2IDLOOPC1998.

� 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 classOb

ject

ives

Page 3: Object-Oriented  Programming  Using C++

3IDLOOPC1998.

Template Functionstemplate <class T>void printArray(const T *array, const int count){

for (int i = 0; i < count; i++)cout << array[i] << “ ’;

cout << endl; }

Pg. 650

Page 4: Object-Oriented  Programming  Using C++

4IDLOOPC1998.

Template Functionstemplate <class T>void printArray(const T *array, const int count){

for (int i = 0; i < count; i++)cout << array[i] << “ ’;

cout << endl;}

Pg. 650

Page 5: Object-Oriented  Programming  Using C++

5IDLOOPC1998.

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

Page 6: Object-Oriented  Programming  Using C++

6IDLOOPC1998.

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

Template Functions

Pgs. 650 and 651

Page 7: Object-Oriented  Programming  Using C++

7IDLOOPC1998.

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

Page 8: Object-Oriented  Programming  Using C++

8IDLOOPC1998.

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

Page 9: Object-Oriented  Programming  Using C++

9IDLOOPC1998.

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

Page 10: Object-Oriented  Programming  Using C++

10IDLOOPC1998.

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

Page 11: Object-Oriented  Programming  Using C++

11IDLOOPC1998.

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

Page 12: Object-Oriented  Programming  Using C++

12IDLOOPC1998.

Class Templates-Parameterized Types#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; }

Pgs. 653 and 654

Page 13: Object-Oriented  Programming  Using C++

13IDLOOPC1998.

Class Templates-Parameterized Types#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; }

Pgs. 653 and 654

type symbol

templatekeyword

Page 14: Object-Oriented  Programming  Using C++

14IDLOOPC1998.

Class Templates-Parameterized Types#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; }

Pgs. 653 and 654

Using theType Symbol

Page 15: Object-Oriented  Programming  Using C++

15IDLOOPC1998.

Stack Operations

push - add a plate

Page 16: Object-Oriented  Programming  Using C++

16IDLOOPC1998.

Stack Operations

push - add a platepop - remove a plate

Page 17: Object-Oriented  Programming  Using C++

17IDLOOPC1998.

Stack Operations

Push - add a platepop - remove a plateis empty - there are no plates

Page 18: Object-Oriented  Programming  Using C++

18IDLOOPC1998.

Stack Operations

Push - add a platepop - remove a plateis empty - there are no platesis full - no more room in cabinet

Page 19: Object-Oriented  Programming  Using C++

19IDLOOPC1998.

Stack as an array of integerstop -1size 8

Page 20: Object-Oriented  Programming  Using C++

20IDLOOPC1998.

Stack as an array of integers

Push (5)

5

top 0size 8

Page 21: Object-Oriented  Programming  Using C++

21IDLOOPC1998.

Stack as an array of integers

Push (5)Push (9)

5 9

top 1size 8

Page 22: Object-Oriented  Programming  Using C++

22IDLOOPC1998.

Stack as an array of integers

Push (5)Push (9)Push (3)

5 39

top 2size 8

Page 23: Object-Oriented  Programming  Using C++

23IDLOOPC1998.

Stack as an array of integers

Push (5)Push (9)Push (3)Pop

5 9

top 1size 8

Page 24: Object-Oriented  Programming  Using C++

24IDLOOPC1998.

Stack as an array of integers

Push (5)Push (9)Push (3)PopPop

5

top 0size 8

Page 25: Object-Oriented  Programming  Using C++

25IDLOOPC1998.

Templates - Constructor

// Constructor with default size 10template<class T>Stack<T>::Stack(int s){size = s > 0 && s < 1000 ? s: 10;top = -1;stackPtr = new T[size];

}

Pg. 654

Page 26: Object-Oriented  Programming  Using C++

26IDLOOPC1998.

Templates - Push// Push an element onto the Stack// return 1 if successful, 0 otherwisetemplate<class T>bool Stack<T>::push(const T &pushValue){

if (!isFull( ) ) {stackPtr[++top] = pushValue;return true;

}return false;

}

Pg. 654

Page 27: Object-Oriented  Programming  Using C++

27IDLOOPC1998.

Templates - Pop// Pop an element off the Stacktemplat<class T>bool Stack<T>::pop(T &popValue){if (!isEmpty( ) ) {

popValue = stackPtr[top- -];return true;

}return false;

}#endif

Pg. 654

Page 28: Object-Oriented  Programming  Using C++

28IDLOOPC1998.

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;

}

Page 29: Object-Oriented  Programming  Using C++

29IDLOOPC1998.

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;

}

Class Name

Page 30: Object-Oriented  Programming  Using C++

30IDLOOPC1998.

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;

}

Class NameObject Type

Page 31: Object-Oriented  Programming  Using C++

31IDLOOPC1998.

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;

}

Class NameObject Type

Desired Stack Size

Page 32: Object-Oriented  Programming  Using C++

32IDLOOPC1998.

Q & A

Page 33: Object-Oriented  Programming  Using C++

33IDLOOPC1998.


Recommended