39
Chapter 6 Lists Plus

Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Embed Size (px)

Citation preview

Page 1: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Chapter 6

Lists Plus

Page 2: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What is a Class Template?

• A class template allows the compiler to generate multiple versions of a class type by using type parameters.

• The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets, < >.

Page 3: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

StackType<int> numStack;

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 789

[ 2 ] -56

[ 1 ] 132

items [ 0 ] 5670

ACTUAL PARAMETER

Page 4: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 3456.8

[ 2 ] -90.98

[ 1 ] 98.6

items [ 0 ] 167.87

StackType<float> numStack;ACTUAL PARAMETER

Page 5: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

top 3

[MAX_ITEMS-1] . . .

[ 3 ] Bradley

[ 2 ] Asad

[ 1 ] Rodrigo

items [ 0 ] Max

StackType<string> numStack;ACTUAL PARAMETER

Page 6: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

//--------------------------------------------------------// CLASS TEMPLATE DEFINITION//--------------------------------------------------------#include "ItemType.h" // for MAX_ITEMS and ItemTyp

template<class ItemType> // formal parameter listclass StackType {public:

StackType( ); bool IsEmpty( ) const;bool IsFull( ) const;void Push( ItemType item );void Pop( ItemType item );ItemType Top( );

private:int top;ItemType items[MAX_ITEMS];

};

Page 7: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

//--------------------------------------------------------// SAMPLE CLASS MEMBER FUNCTIONS //--------------------------------------------------------

template<class ItemType> // formal parameter listStackType<ItemType>::StackType( ){

top = -1;}template<class ItemType> // formal parameter list void StackType<ItemType>::Push ( ItemType newItem ){

if (IsFull()) throw FullStack(); top++;

items[top] = newItem; // STATIC ARRAY IMPLEMENTATION

}

Page 8: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Using class templates

The actual parameter to the template is a data type. Any type can be used, either built-in or user-defined.

Templates are instantiated at run time. Can you see how this might cause a

problem?

Page 9: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

When creating class template• Put .h and .cpp in same file or

• Have .h include .cpp file

Page 10: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What is a Circular Linked List?

• A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

Page 11: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

External Pointer to the Last Node

Page 12: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What is a Doubly Linked List?

• A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

Page 13: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Linking the New Node into the List

Page 14: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Deleting from a Doubly Linked List

Page 15: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What are Header and Trailer Nodes?

• A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key.

• A Trailer Node is a node at the end of a list that contains a key larger than any possible key.

• Both header and trailer are placeholding nodes used to simplify list processing.

Page 16: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Recall Definition of Stack

• Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack.

• A stack is a LIFO “last in, first out” structure.

Page 17: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

class StackType<int>

StackType

Top

Pop

Push

IsFull

IsEmpty Private data:

topPtr

~StackType

20 30

Page 18: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What happens . . .

• When a function is called that uses pass by value for a class object like our dynamically linked stack?

Page 19: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

19

Pass by value makes a shallow copy

20 30

StackType<int> myStack; // CLIENT CODE . . .

MyFunction( myStack ); // function call

Private data: 7000 6000

topPtr 7000

Private data:

topPtr 7000

myStack momeStack

shallow copy

Page 20: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What’s the difference?

• A shallow copy shares the pointed to data with the original class object.

• A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.

Page 21: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Making a deep copy

20 30

Private data: 7000 6000

topPtr 7000someStack

20 30

Private data: 5000 2000

topPtr 5000

myStack

deep copy

Page 22: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

// FUNCTION CODE

template<class ItemType>

void MyFunction( StackType<ItemType> SomeStack )

// Uses pass by value

{

ItemType item;

SomeStack.Pop(item); .

.

.

}

Suppose MyFunction Uses Pop

What happens in a shallow copy?

Page 23: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

23

MyStack.topPtr is left dangling

? 30

StackType<int> myStack; // CLIENT CODE . . .

MyFunction( myStack );

Private data:

topPtr 6000

myStack someStack

shallow copy

Private data: 7000 6000

topPtr 7000

Page 24: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

24

MyStack.topPtr is left dangling

? 30

Private data:

topPtr 6000

myStack someStack

shallow copy

Private data: 7000 6000

topPtr 7000

Notice that the shallow copy and the actual parameter myStack, have changed!

Page 25: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

As a result . . .

• This default method used for pass by value is not the best way when a data member points to dynamic data.

• Instead, you should write what is called a copy constructor, which makes a deep copy of the dynamic data in a different memory location.

Page 26: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

More about copy constructors

• When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value.

• You do not call the copy constructor.

• Like other constructors, it has no return type.

• Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.

Page 27: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Copy Constructor

Copy constructor is a special member function of a class that is implicitly called in these three situations:

• passing object parameters by value,• initializing an object variable in a

declaration, • returning an object as the return value of

a function.

Page 28: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

// DYNAMICALLY LINKED IMPLEMENTATION OF STACK template<class ItemType>class StackType {public: StackType( );

// Default constructor.// Post: Stack is created and empty.

StackType( const StackType<ItemType>& anotherStack );// Copy constructor.// Implicitly called for pass by value.. ..

~StackType( ); // Destructor.// Post: Memory for nodes has been deallocated.

private:NodeType<ItemType>* topPtr;

};

Page 29: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

CLASS CONSTRUCTOR

CLASS COPY CONSTRUCTOR

CLASS DESTRUCTOR

Classes with Data Member Pointers Need

Page 30: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

template<class ItemType> // COPY CONSTRUCTORStackType<ItemType>::StackType( const StackType<ItemType>& anotherStack ){ NodeType<ItemType>* ptr1;

NodeType<ItemType>* ptr2;if ( anotherStack.topPtr == NULL )

topPtr = NULL;else // allocate memory for first node{ topPtr = new NodeType<ItemType>;

topPtr->info = anotherStack.topPtr->info;ptr1 = anotherStack.topPtr->next;ptr2 = topPtr;while ( ptr1 != NULL ) // deep copy other nodes{ ptr2->next = new NodeType<ItemType>;

ptr2 = ptr2->next;ptr2->info = ptr1->info;ptr1 = ptr1->next;

}ptr2->next = NULL;

}}

Page 31: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What About the Assignment Operator?

• The default method used for assignment of class objects makes a shallow copy.

• If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.

Page 32: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

// DYNAMICALLY LINKED IMPLEMENTATION OF STACK template<class ItemType>class StackType {public:

StackType( ); // Default constructor.StackType(const StackType<ItemType>& anotherStack );// Copy constructor.void operator= ( StackType<ItemType> );// Overloads assignment operator.. ..

~StackType( ); // Destructor.

private:NodeType<ItemType>* topPtr;

};

Page 33: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

C++ Operator Overloading Guides

1 All operators except these :: . sizeof ?: may be overloaded.

2 At least one operand must be a class instance.3 You cannot change precedence, operator symbols, or

number of operands.4 Overloading ++ and -- requires prefix form use by

default, unless special mechanism is used. 5 To overload these operators = ( ) [ ] member

functions (not friend functions) must be used. 6 An operator can be given multiple meanings if the

data types of operands differ.

Page 34: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Using Overloaded Binary operator+

When a Member Function was definedmyStack + yourStack

myStack.operator+(yourStack)

When a Friend Function was defined

myStack + yourStack

operator+(myStack, yourStack)

Page 35: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

A Sorted list Stored in an Array of Nodes

Page 36: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

An Array with Linked List of Values and Free Space

Page 37: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

An Array with Three Lists (Including the Free List)

Page 38: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

Polymorphism with Virtual Functions

formalParameter.MemberFunction(...);then1.If MemberFunction is not a virtual function, the type of the formal parameter determines which function to call. (Static binding is used.)

2.If MemberFunction is a virtual function, the type of the actual parameter determines which function to call. (Dynamic binding is used.).

Page 39: Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters

What are the advantages of a circular doubly linked list?