Chapter16 - [ Viewing Hints ] [ Exercise Solutions ] [...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: [ Viewing Hints ] [ Exercise Solutions ] [ Volume 2 ] [ Free Newsletter ] [ Seminars ] [ Seminars on CD ROM ] [ Consulting ] Thinking in C++, 2nd ed. Volume 1 2000 by Bruce Eckel [ Previous Chapter ] [ Table of Contents ] [ Index ] [ Next Chapter ] 16: Introduction to Templates Inheritance and composition provide a way to reuse object code. The template feature in C++ provides a way to reuse source code. Although C++ templates are a general-purpose programming tool, when they were introduced in the language, they seemed to discourage the use of object-based container-class hierarchies (demonstrated at the end of Chapter 15). For example, the Standard C++ containers and algorithms (explained in two chapters of Volume 2 of this book, downloadable from www.BruceEckel.com ) are built exclusively with templates and are relatively easy for the programmer to use. This chapter not only demonstrates the basics of templates, it is also an introduction to containers, which are fundamental components of object-oriented programming and are almost completely realized through the containers in the Standard C++ Library. Youll see that this book has been using container examples the Stash and Stack throughout, precisely to get you comfortable with containers; in this chapter the concept of the iterator will also be added. Although containers are ideal examples for use with templates, in Volume 2 (which has an advanced templates chapter) youll learn that there are many other uses for templates as well. Containers Suppose you want to create a stack, as we have been doing throughout the book. This stack class will hold int s, to keep it simple: //: C16:IntStack.cpp // Simple integer stack //{L} fibonacci #include "fibonacci.h" #include "../require.h" #include <iostream> using namespace std; class IntStack { enum { ssize = 100 }; int stack[ssize]; int top; public : IntStack() : top(0) {} void push( int i) { require(top < ssize, "Too many push()es" ); stack[top++] = i; } int pop() { require(top > 0, "Too many pop()s" ); return stack[--top]; } }; int main() { IntStack is; // Add some Fibonacci numbers, for interest: for ( int i = 0; i < 20; i++) is.push(fibonacci(i)); // Pop & print them: for ( int k = 0; k < 20; k++) cout << is.pop() << endl; } ///:~ The class IntStack is a trivial example of a push-down stack. For simplicity it has been created here with a fixed size, but you can also modify it to automatically expand by allocating memory off the heap, as in the Stack class that has been examined throughout the book....
View Full Document

This note was uploaded on 08/10/2011 for the course IT 331 taught by Professor Nevermind during the Spring '11 term at King Abdulaziz University.

Page1 / 45

Chapter16 - [ Viewing Hints ] [ Exercise Solutions ] [...

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

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