IUPUI CS 265 - LECTURE NOTES (9 pages)

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

LECTURE NOTES



Previewing pages 1, 2, 3 of actual document.

View the full content.
View Full Document
View Full Document

LECTURE NOTES

111 views

Lecture Notes


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

Unformatted text preview:

Department of Computer and Information Science School of Science IUPUI Fall 2003 Templates Dale Roberts Lecturer Computer Science IUPUI E mail droberts cs iupui edu 01 14 19 Dale Roberts 1 Templates A Template Defines the Content of a Family of Data Types Two Types Function and Class Templates 01 14 19 Dale Roberts 2 Function Templates Specifies a Generic Class and Uses this Class in the Function Algorithm Compiler Creates Appropriate Function Definition Using the Argument Specified During a Function Call Invocation 01 14 19 Dale Roberts 3 Function Template Example include stream h Template Function Definition template class T T maximum T t1 T t2 if t1 t2 return t1 else return t2 main int a 10 b 15 float c 20 0 d 25 5 cout Maximum Integer maximum a b endl cout Maximum Float maximum c d endl ERROR Parameter Type Mismatch No Conversion cout Maximum maximum a d endl Explicit Prototype for Forced Conversion float maximum float float cout Maximum maximum a d endl FINE 01 14 19 Dale Roberts 4 Class Template Specifies a Generic Class and Uses it in the Algorithm Proper Arguments Must be Supplied Before Creating an Instance or Object Using the Template Class 01 14 19 Dale Roberts 5 Class Template Stack Example A Stack of Arbitrary Elements template class T class stack T head T tail int sz public stack int s head tail new T sz s stack delete head void push T a tail a T pop return tail int size const return tail head main Stack of 100 Characters stack char char stack 100 char stack push R cout Stack Size char stack size endl Stack of 50 Integers stack int int stack 50 int stack push 10 cout Top int stack pop endl 01 14 19 Dale Roberts 6 Class Template Stack Example Cont d OUTPUT WILL Stack Size Top 10 01 14 19 BE 1 Dale Roberts 7 Container Classes Useful Kind of Classes These Hold Objects of Some Other Type Examples Lists Arrays Sets A Set can Contain Integers Characters Floats Strings Structures Representing Citizens 01 14 19 Dale Roberts 8 Properties of Container Classes



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view LECTURE NOTES 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 LECTURE NOTES 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?