1 / 7

Department of Computer and Information Science, School of Science, IUPUI

Department of Computer and Information Science, School of Science, IUPUI. Templates. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Templates. A Template Defines the Content of a Family of Data Types Two Types -- Function and Class Templates .

burgess
Download Presentation

Department of Computer and Information Science, School of Science, IUPUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI Templates Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Templates • A Template Defines the Content of a Family of Data Types • Two Types -- Function and Class Templates

  3. 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

  4. 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 }

  5. 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

  6. 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;

  7. Class Template -- Stack Example -- Cont'd OUTPUT WILL BE ------ ---- -- Stack Size: 1 Top: 10

More Related