topic05_Templates

Topic05_Templates - Template Classes in C CS 1037a – Topic 5 Related materials • Sec 6.1-6.2 Templated functions and classes from Main and

Info iconThis preview shows pages 1–9. 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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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: Template Classes in C++ CS 1037a – Topic 5 Related materials • Sec. 6.1-6.2: Templated functions and classes from Main and Savitch “Data Structures & other objects using C++” from CS1037 labs • Lab 4, using template Bag<Item> 5-2 Problems with Our Data Structures So Far • Suppose that a program needs two kinds of bags, say a bag of int s and a bag of double s • We then need two different bag classes that are nearly identical • It would be better if we could write the bag code once, and let C++ generate two slightly different classes based on the type of data to be stored in each 5-3 Problem Solution: Templates • A C++ template class is a blueprint for a family of classes • Example : A template class for bags: • The template class defines a bag of elements once, in a general way • Programs using bags can then instantiate separate bag classes for as many specific types of elements as necessary 5-4 Problem Solution: Templates • When an object of a data structure type is instantiated, the type of data object it stores is specified • If the class for that object does not already exist, the class is instantiated at compile time from a template • Then the object from that class is instantiated 5-5 Problem Solution: Templates • Template class for a data structure uses a .h file and, instead of a .cpp file, a .template file 5-6 Bags With and Without Templates // BagOfItems.h (Topic 4) #define CAPACITY 50 typedef ExistingClassName Item; class BagOfItems { public: BagOfItems(unsigned int max_capacity=CAPACITY); ~BagOfItems( ); // cont’d.. // Bag.h (template class) #define CAPACITY 50 template <class Item> class Bag { public: Bag(unsigned int max_capacity=CAPACITY); ~Bag( ); // cont’d.. 5-7 Bags With and Without Templates // …BagOfItems.h cont’d void add( Item value ); Item getOne( ); bool isEmpty( ) const; bool isFull( ) const; unsigned int currentSize( ) const; unsigned int capacity( ) const; // cont’d.. // …Bag.h cont’d (template) void add( Item value ); Item getOne( ); bool isEmpty( ) const; bool isFull( ) const; unsigned int currentSize( ) const; unsigned int capacity( ) const; // cont’d.....
View Full Document

This note was uploaded on 10/26/2009 for the course CS cs1037 taught by Professor Yuri during the Fall '07 term at UWO.

Page1 / 26

Topic05_Templates - Template Classes in C CS 1037a – Topic 5 Related materials • Sec 6.1-6.2 Templated functions and classes from Main and

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

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