Lecture13

Lecture13 - class instantiation, not per class....

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS11600: Introduction to Computer Programming (C++) Lecture 13 Svetlozar Nestorov University of Chicago 2/7/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 2 Outline ± Template classes ± Example: generic Stack class ± Static members ± Constant parameters ± Example: a generic List of arrays ± Standard Template Library (STL) 2/7/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 3 Template Classes ± Generic, type-independent classes. ± Common use as container classes: • list, set, bag, queue ± Basic syntax: template <class T> class MyClass { … }; ± Template may involve multiple classes. 2/7/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 4 Instantiation ± Fill in types and use as a regular class: MyClass<int> obj1; MyClass<char*> obj2; MyClass<double> scores[10]; MyClass<int> *n = new MyClass<int>; ± Example: generic Stack class. 2/7/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 5 Static Data Members ± One static data member per
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: class instantiation, not per class. Initialization syntax: template &lt;class T&gt; Type MyClass&lt;T&gt;::name = value; A different initialization for specific type: Type MyClass&lt;int&gt;::name = value; 2/7/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 6 Constant Parameters In addition to classes, template classes may depend on a constant parameter. template &lt;class T, Type val&gt; class MyClass { }; Example: Stack with max depth: template &lt;class T, int maxDepth&gt; class Stack { }; 2 2/7/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 7 Standard Template Library Generic container classes: vector, list, deque, set, multiset, map, mltimap, stack, queue, priority_queue (last 3 are adaptors). Iterators to navigate the container classes: iterator, reverse_iterator, and const_iterator, const_reverse_iterator No iterators for adaptors. Generic algorithms:...
View Full Document

Page1 / 2

Lecture13 - class instantiation, not per class....

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online