templates mark hennessy dept computer scicene nui maynooth c++ workshop 18 th – 22 nd september...

19
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Upload: hector-lane

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Templates

Mark Hennessy

Dept Computer Scicene

NUI Maynooth

C++ Workshop

18th – 22nd September 2006

Page 2: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Polymorphism and Templates

Our Shape pointers can point to any of it's subclass.

Shape *s;

Circle *c = new Circle(6);

s = c;

It is impossible to tell at compile time what Shape will be

pointing to at compile time so how can we find out what

Shape is really pointing to?

Through a generic templated function!

Page 3: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

dynamic_cast<>()

This function allows us to determine the type of a pointer at

run-time.

dynamic_cast < > ( )

fn_name template_parameter pointer_instance

Shape *s = c;

Circle *c1 = dynamic_cast<Circle*>(s);

Page 4: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Generic Classes and C++ Templates

A generic class can be thought of as an abstract specification of a set of classes

It is a recipe for producing classes

An instance of a generic class is a class

(An instance of a class is an object)

In C++, generic classes are defined by templates

Templates can be used to produce functions and classes

C++’s template facility is very powerful (surprise surprise!) but it can become very complicated when combined with other language features

Page 5: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Function Templates … a swap...

template <class T>void swap (T& x, T& y){T temp = x; x = y; y = temp;}

int main (){int a = 1, b =2;cout << "a is "<< a<<", b is "<<b<<endl;swap (a,b);cout << "a is "<< a<<", b is "<<b<<endl;}

a is 1, b is 2a is 2, b is 1

This outputs:

This example can now be extended to show the power of the generic swap function

Page 6: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Function Templates … a swap...

template <class T>void swap (T& x, T& y){T temp = x; x = y; y = temp;}int main (){int a = 1, b =2;cout << "a is "<< a<<", b is "<<b<<endl;swap (a,b);cout << "a is "<< a<<", b is "<<b<<endl;char c = 'c', d = 'd';cout << "c is "<< c<<", d is "<<d<<endl;swap (c,d);cout << "c is "<< c<<", d is "<<d<<endl;}

a is 1, b is 2a is 2, b is 1c is c, d is dc is d, d is c

This outputs:

Page 7: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Question: Why should I write Stack, List, Set, Sort, Search, etc?

Answer: don’t!!!

use the C++ Standard Library.

Heart of the library: STL

Page 8: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

The standard C++ library has:

ContainersIterators to “run throughrun through” the containersAlgorithms that work on containers: find, sort, etc.container adapters: stack, queue, priority_queueiterator adaptersstrings

Page 9: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

containers

grow as needed -- good to manage data, as used in typical programs:

programs start with an empty container, read or compute data, store it, access it, store some more…

built in arrays don’t grow

all 3 sequential containers have push_backpush_back

Page 10: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Some functions for sequential containers:

push_back(), pop_back()

push_front(), pop_front()

size()

begin() -- points to first item in vector

end() -- points one past end of vector

iterator: pointer to an item in the vector

Page 11: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

C++ strings are containers:better than C strings

Memory managed for you:

C++ ==> string s(“dog”)

C ==> s = new char[4]; strcpy(s, “dog”)

easier to use

C++ ==> s <= t

C ==> strcmp(s, t) <= 0

less error prone than C strings

Page 12: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Iterators

step through a container, independent of internal structure

small but common interface for any container

every container has its own iterator

interface for iterators almost the same as pointers: *, ++, --

Page 13: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Iterator Use

list<std::string> MyList;

for( list<std::string>::iterator ptr = MyList.begin() ; ptr ! = MyList.end(); ptr++;

)

{

std::cout<<*ptr;

}

Page 14: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

#include <iostream>#include <vector>const int MAX = 1024;

void print(const vector<int> & n) { for (int i = 0; i < n.size(); ++i) cout << n[i] << endl;}int main() { vector<int> items; cout << “size of items is: “ << items.size() << endl; items.push_back(rand() % 100); cout << “size of items is: “ << items.size() << endl; for (int i = 0; i < MAX; ++i) items.push_back(rand() %100); cout << “size of items is: “ << items.size() << endl; print(items);}

14

Page 15: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Algorithms

containeralgorithmiterator iterator container

Algorithms process the elements of a containerAlgorithms use iterators

Separation of data and operations:an algorithm can be written once and work witharbitrary containers because iterators have acommon interface.

Page 16: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Namespaces

Used by Standard C++ Library

16

Page 17: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Name clashes

current software trends: build libraries, modules, components

when combined, names can clash

Namespaces solve this problem

example:namespace NS { class Student {}; void print(); int x;}

Page 18: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

3 ways to access namespace items

with the scope qualifier: NS::x;

qualifier with using: using NS::x;

the using directive:

using namespace NS;

Page 19: Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

C++ Standard library -- stdAll identifiers are in namespace std

Some compilers enforce use of std namespace

gcc’s C++ compiler does not enforce std until 3.0.4

std::cout << std::hex << 3.4 << std::endl;