Today’s Objectives

39
1 Today’s Objectives Announcements Homework #4 is due on Monday, 17-Jul Homework #5 is posted. It’s due on 24-Jul, and no late projects will be accepted Review Quiz #3 UML sequence diagrams Review of overloading Templates (Ch. 14) Review of function templates Class templates Non-type parameters Bonus Lab #7 12-Jul-2006

description

12-Jul-2006. Today’s Objectives. Announcements Homework #4 is due on Monday, 17-Jul Homework #5 is posted. It’s due on 24-Jul, and no late projects will be accepted Review Quiz #3 UML sequence diagrams Review of overloading Templates (Ch. 14) Review of function templates - PowerPoint PPT Presentation

Transcript of Today’s Objectives

1

Today’s ObjectivesToday’s Objectives

Announcements• Homework #4 is due on Monday, 17-Jul• Homework #5 is posted.

It’s due on 24-Jul, and no late projects will be accepted

Review Quiz #3

UML sequence diagrams

Review of overloading

Templates (Ch. 14)• Review of function templates• Class templates• Non-type parameters

Bonus Lab #7

12-Jul-200612-Jul-2006

2

Review Quiz #3Review Quiz #3

3

UML Sequence DiagramsUML Sequence Diagrams

4

UML Sequence DiagramUML Sequence Diagram

Shows the interaction between objects in your program

One of two “UML interaction diagrams”• The other UML interaction diagram is

called a “collaboration diagram”• The sequence diagram does a better job of

showing the timing of the interactions

Sequence Diagrams (Deitel, 63, 382; Stevens, 117)Sequence Diagrams (Deitel, 63, 382; Stevens, 117)

5

UML Sequence DiagramShowing the interaction in the DemoLibrary program

UML Sequence DiagramShowing the interaction in the DemoLibrary program

book("Java How to Program",4)

Library user

myLibrary :Library

myList :LibraryItemList

book :Book

*pItem :LibraryItem

addLibraryItem(item)

push_back(item)

book

new LibraryItem(item)

pItem

Sequence Diagrams (Deitel, 63, 382; Stevens, 117)Sequence Diagrams (Deitel, 63, 382; Stevens, 117)

6

UML Sequence DiagramShowing the interaction in the DemoLibrary program

UML Sequence DiagramShowing the interaction in the DemoLibrary program

book("Java How to Program",4)

Library user

myLibrary :Library

myList :LibraryItemList

book :Book

*pItem :LibraryItem

addLibraryItem(item)

push_back(item)

book

new LibraryItem(item)

pItem

Sequence Diagrams (Deitel, 63, 382; Stevens, 117)Sequence Diagrams (Deitel, 63, 382; Stevens, 117)

Objects in the program

“Actor” that uses the program

7

UML Sequence DiagramShowing the interaction in the DemoLibrary program

UML Sequence DiagramShowing the interaction in the DemoLibrary program

book("Java How to Program",4)

Library user

myLibrary :Library

myList :LibraryItemList

book :Book

*pItem :LibraryItem

addLibraryItem(item)

push_back(item)

book

new LibraryItem(item)

pItem

Sequence Diagrams (Deitel, 63, 382; Stevens, 117)Sequence Diagrams (Deitel, 63, 382; Stevens, 117)

Time passes as we move from top to bottom.

Lifeline of the object

8

UML Sequence DiagramShowing the interaction in the DemoLibrary program

UML Sequence DiagramShowing the interaction in the DemoLibrary program

book("Java How to Program",4)

Library user

myLibrary :Library

myList :LibraryItemList

book :Book

*pItem :LibraryItem

addLibraryItem(item)

push_back(item)

book

new LibraryItem(item)

pItem

Sequence Diagrams (Deitel, 63, 382; Stevens, 117)Sequence Diagrams (Deitel, 63, 382; Stevens, 117)

Call to a member function

Return value from the function

9

Problem with the DemoLibraryProblem with the DemoLibrary

10

UML Sequence DiagramShowing the interaction in the DemoLibrary program

UML Sequence DiagramShowing the interaction in the DemoLibrary program

book("Java How to Program",4)

Library user

myLibrary :Library

myList :LibraryItemList

book :Book

*pItem :LibraryItem

addLibraryItem(item)

push_back(item)

book

new LibraryItem(item)

pItem

DemoLibrary ProblemDemoLibrary Problem

The pointer that is loaded into the “myData” array of myList points to a base-class object, but we want it to point to a derived-class object. Then we can use polymorphism in the “printList” member function.

11

UML Sequence DiagramShowing the interaction in the DemoLibrary program

UML Sequence DiagramShowing the interaction in the DemoLibrary program

book("Java How to Program",4)

Library user

myLibrary :Library

myList :LibraryItemList

book :Book

*pItem :Book

addLibraryItem(book)

push_back(book)

book

new Book(book)

pItem

One way to fix the problem is to use overloaded member functions in these places.

Then we’ll know which derived class to allocate space for.

DemoLibrary ProblemDemoLibrary Problem

12

Review: Function OverloadingReview: Function Overloading

Using the same name for two or more different functions To perform operations that are similar, but that must be

implemented differently for different inputs

double maximum( double x, double y ){ return ( x > y ) ? x : y;}

int maximum( int x, int y ){ return ( x > y ) ? x : y;}

double maximum( double x, double y, double z ){double temp = ( x > y ) ? x : y;return ( temp > z ) ? temp : z;

}

Review (Deitel, 283)Review (Deitel, 283)

13

double maximum( double x, double y ){ return ( x > y ) ? x : y;}

int maximum( int x, int y ){ return ( x > y ) ? x : y;}

double maximum( double x, double y, double z ){double temp = ( x > y ) ? x : y;return ( temp > z ) ? temp : z;

}

Review: Function SignaturesReview: Function Signatures

Function’s name + parameter types Overloaded functions must have different signatures Return types can be the same or different

Review (Deitel, 283)Review (Deitel, 283)

signature

signature

signature

14

Function Overloading in DemoLibrary

Function Overloading in DemoLibrary

Member functions can also be overloaded Overloaded member functions must have different signatures

class Library{public:

void addLibraryItem( Book& book ){myList.push_back(book);

}

void addLibraryItem( Journal& journal ){myList.push_back(journal);

}

//...

DemoLibrary ProblemDemoLibrary Problem

15

Function Overloading in DemoLibrary

Function Overloading in DemoLibrary

class LibraryItemList{public:

void push_back( Book &book ){if( sz < capacity ){

LibraryItem *pItem = new Book(book);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}void push_back( Journal &journal ){

if( sz < capacity ){LibraryItem *pItem = new Journal(journal);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}//...

Now we can use polymorphism because the base-class pointer is pointing to a derived-class object

DemoLibrary ProblemDemoLibrary Problem

16

TemplatesTemplates

Chapter 14

17

ReusabilityReusability

An important design goal of software engineering

Advantages• Saves time• Better to use code that is already tested

Templates (Deitel, 749)Templates (Deitel, 749)

18

TemplatesTemplates

An important feature in C++

Supports software reuse• We can write one class or function, and then

use it with many data types

Generic programming• Using class templates

Many class templates are available in the Standard Template Library (STL)

Templates (Deitel, 749)Templates (Deitel, 749)

19

Review: Inappropriate OverloadingReview: Inappropriate Overloading

These two functions have different signatures, so overloading is allowed

However, the implementation is the same A better approach is to use a function template

Templates (Deitel, 283)Templates (Deitel, 283)

double maximum( double x, double y ){ return ( x > y ) ? x : y;}

int maximum( int x, int y ){return ( x > y ) ? x : y;

}

20

Review: Function TemplatesReview: Function Templates

Allow us to write a single function definition that can be used with any data type

The program logic inside the function must be the same for every data type

Begins with “template < typename T >” The parameters are placeholders for the data type – the

compiler will make the substitution later

Templates (Deitel, 283)Templates (Deitel, 283)

template < typename T > //use either “class” or “typename”T maximum( T x, T y ){

return ( x > y ) ? x : y;}

21

Review: Using a Function TemplateReview: Using a Function Template

#include <iostream>using namespace std;

template < typename T >T maximum( T x, T y ){

return ( x > y ) ? x : y;}

int main(){ int a = 1, b = 2; cout << maximum(a,b) << endl;

double x = 3.0, y = 4.0; cout << maximum(x,y) << endl;}

Templates (Deitel, 283)Templates (Deitel, 283)

The compiler automatically detects the datatype of the argument and substitutes that datatype at every location of ‘T’

22

Using Function Templatesin DemoLibrary

Using Function Templatesin DemoLibrary

Function templates can also be used inside a class

For example, in LibraryItemList, we used overloaded member functions

• Each has a different derived-class data type as parameter

• The code inside these member functions is almost identical except for the data type of the parameters

• This is a good place to use a function template.

TemplatesTemplates

23

Using Function Templatesin DemoLibrary

Using Function Templatesin DemoLibrary

class LibraryItemList{public:

void push_back( Book &book ){if( sz < capacity ){

LibraryItem *pItem = new Book(book);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}void push_back( Journal &journal ){

if( sz < capacity ){LibraryItem *pItem = new Journal(journal);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}

The code is almost the same, except for the derived-class data type

TemplatesTemplates

1. Start with the overloaded member functions

24

Using Function Templatesin DemoLibrary

Using Function Templatesin DemoLibrary

class LibraryItemList{public:

void push_back( Book &book ){if( sz < capacity ){

LibraryItem *pItem = new Book(book);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}

TemplatesTemplates

2. Delete all but one of the overloaded member functions

25

Using Function Templatesin DemoLibrary

Using Function Templatesin DemoLibrary

class LibraryItemList{public:

template < typename Derived >void push_back( Book &book ){

if( sz < capacity ){LibraryItem *pItem = new Book(book);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}

TemplatesTemplates

3. Add this line

26

Using Function Templatesin DemoLibrary

Using Function Templatesin DemoLibrary

class LibraryItemList{public:

template < typename Derived >void push_back( Book &book ){

if( sz < capacity ){LibraryItem *pItem = new Book(book);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}

TemplatesTemplates

4. Find all the locations of the derived-class data type

27

Using Function Templatesin DemoLibrary

Using Function Templatesin DemoLibrary

class LibraryItemList{public:

template < typename Derived >void push_back( Derived &item ){

if( sz < capacity ){LibraryItem *pItem = new Derived(item);myData[sz] = pItem;sz++;

}else cout << "FULL\n";

}

TemplatesTemplates

5. Substitute the word “Derived” for the derived class data type

28

Using LibraryItemList with any DataUsing LibraryItemList with any Data

So far, our LibraryItemList class will do a good job of containing data of the type LibraryItem

What happens if we want to store something else?• Brute force solution – Write a different

class for every type of data we might want to store – RentalItemList, CustomerList, etc.

What’s the disadvantage of that approach?

TemplatesTemplates

29

Class TemplatesClass Templates

Better approach• Write a class template• A class template can store any kind of data

Instead of making a list of LibraryItems, we can make a list of “anything”

The behavior for all the data types will be the same and we only need to write the code once

Called “generic programming”

Templates (Deitel, 754)Templates (Deitel, 754)

30

Class TemplatesClass Templates

Allow us to use one class implementation for many different data types

Syntax

template <typename Object>class List{private:Object *pMyData;

“typename” or “class”

“Object” or any word – the compiler will replace this word

with an actual data type

Templates (Deitel, 754)Templates (Deitel, 754)

31

Using a Class Templatein DemoLibrary

Using a Class Templatein DemoLibrary

class LibraryItemList {public:

LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){myData = new LibraryItem*[CAPACITY];

}~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i];

delete[] myData; }int size(){ return sz; }bool empty(){ return sz == 0; }void printList(){

if( empty() ) cout << "List is empty\n";else{

for( int i=0; i<sz; ++i )cout << myData[i]->toString() << '\n';

}}

private:const int CAPACITY;int sz;LibraryItem **myData;

};

1. Start with the class that stores a specific data type

Templates (Deitel, 754)Templates (Deitel, 754)

32

Using a Class Templatein DemoLibrary

Using a Class Templatein DemoLibrary

template < typename Base >class LibraryItemList {public:

LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){myData = new LibraryItem*[CAPACITY];

}~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i];

delete[] myData; }int size(){ return sz; }bool empty(){ return sz == 0; }void printList(){

if( empty() ) cout << "List is empty\n";else{

for( int i=0; i<sz; ++i )cout << myData[i]->toString() << '\n';

}}

private:const int CAPACITY;int sz;LibraryItem **myData;

};

2. Add this line

Templates (Deitel, 754)Templates (Deitel, 754)

33

Using a Class Templatein DemoLibrary

Using a Class Templatein DemoLibrary

template < typename Base >class LibraryItemList {public:

LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){myData = new LibraryItem*[CAPACITY];

}~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i];

delete[] myData; }int size(){ return sz; }bool empty(){ return sz == 0; }void printList(){

if( empty() ) cout << "List is empty\n";else{

for( int i=0; i<sz; ++i )cout << myData[i]->toString() << '\n';

}}

private:const int CAPACITY;int sz;LibraryItem **myData;

};

3. Find all the locations of the data type of the elements in the list

Templates (Deitel, 754)Templates (Deitel, 754)

34

Using a Class Templatein DemoLibrary

Using a Class Templatein DemoLibrary

template < typename Base >class LibraryItemList {public:

LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){myData = new Base*[CAPACITY];

}~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i];

delete[] myData; }int size(){ return sz; }bool empty(){ return sz == 0; }void printList(){

if( empty() ) cout << "List is empty\n";else{

for( int i=0; i<sz; ++i )cout << myData[i]->toString() << '\n';

}}

private:const int CAPACITY;int sz;Base **myData;

};

4. Substitute “Base” for the data type of the elements in the list

Templates (Deitel, 754)Templates (Deitel, 754)

35

Using an External Definition of a Member Function in a TemplateUsing an External Definition of a Member Function in a Template

template < typename Base >class LibraryItemList {public:

LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){myData = new Base*[CAPACITY];

}~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i];

delete[] myData; }int size(){ return sz; }bool empty(){ return sz == 0; }void printList();

private:const int CAPACITY;int sz;Base **myData;

};template < typename Base >void LibraryItemList<Base>::printList(){

if( empty() ) cout << "List is empty\n";else{

for(int i=0; i<sz; ++i) cout << myData[i]->toString();}}

1. Put this line above every function definition

2. Add “Base” wherever the data type should appear

Templates (Deitel, 754)Templates (Deitel, 754)

36

Caution!Caution!

The entire class template must go into the header file!

In C++ a common technique is to put the declaration of a class in the header file and the definition in a cpp file, but it does not work with class templates

Templates (Deitel, 754)Templates (Deitel, 754)

37

Instantiating a List with a Template Class

Instantiating a List with a Template Class

Include it in your program#include "List.h"

Instantiate a list with the default sizeList<LibraryItem> myLibraryItemList;

Instantiate a list with a specific sizeList<LibraryItem> smallList(10);

Name of the class Type of data in it Name of the list object

Size of the list

Templates (Deitel, 754)Templates (Deitel, 754)

38

Non-Type ParametersNon-Type Parameters

The parameters of a class template can be non-type parameters

Exampletemplate < typename Base, int CAP = 32 >class List{public:

List(int cap=CAP):capacity(cap),sz(0) { myData = new Base*[capacity]; }~List() {

for( int i=0; i<sz; ++i ) delete myData[i];delete [] myData; }

bool insert( Base* pItem );private:

int sz;int capacity;Base **myData;

};

Templates (Deitel, 754)Templates (Deitel, 754)

39

ReferencesReferences

Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005.

Stevens, P., with R. Pooley, Using UML, Software Engineering with Objects and Components, Update Edition. Essex, England: Addison Wesley, 2000.