IUPUI CS 265 - C++ Templates (15 pages)

Previewing pages 1, 2, 3, 4, 5 of 15 page document View the full content.
View Full Document

C++ Templates



Previewing pages 1, 2, 3, 4, 5 of actual document.

View the full content.
View Full Document
View Full Document

C++ Templates

111 views

Lecture Notes


Pages:
15
School:
Indiana University - Purdue University Indianapolis
Course:
Cs 265 - Advanced Programming

Unformatted text preview:

Chapter 22 C Templates Outline 22 1 22 2 22 3 22 4 22 5 22 6 Introduction Class Templates Class Templates and Non type Parameters Templates and Inheritance Templates and friends Templates and static Members 2000 Prentice Hall Inc All rights reserved 22 1 Introduction Templates Easily create a large range of related functions or classes Function template the blueprint of the related functions Template function a specific function made from a function template 2000 Prentice Hall Inc All rights reserved 22 2 Class Templates Class templates Allow type specific versions of generic classes Format template class T class ClassName definition Need not use T any identifier will work To create an object of the class type ClassName type myObject Example Stack double doubleStack 2000 Prentice Hall Inc All rights reserved 22 2 Class Templates II Template class functions Declared normally but preceded by template class T Generic data in class listed as type T Binary scope resolution operator used Template class function definition template class T MyClass T MyClass int size myArray new T size Constructor definition creates an array of type T 2000 Prentice Hall Inc All rights reserved 1 Fig 22 3 tstack1 h 2 Class template Stack 3 ifndef TSTACK1 H 4 define TSTACK1 H Outline 5 6 template class T 7 class Stack 8 public 9 Stack int 10 1 Class template definition default constructor stack size 10 10 Stack delete stackPtr destructor 11 bool push const T push an element onto the stack 12 bool pop T pop an element off the stack 13 private 14 int size of elements in the stack 15 int top location of the top element 16 T stackPtr pointer to the stack 17 18 bool isEmpty const return top 1 utility 19 bool isFull const return top size 1 functions 20 21 22 Constructor with default size 10 23 template class T 24 Stack T Stack int s 25 26 size s 0 s 10 27 top 1 28 Stack is initially empty stackPtr new T size allocate space for elements 29 2000 Prentice Hall Inc All rights reserved 1 1 Function



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view C++ Templates and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++ Templates and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?