1 / 17

Templates in C++: Introduction and Usage

This lecture covers the concept of templates in C++ and demonstrates how to use template classes and functions. It also discusses function overloading and the use of templates in the Standard Template Library (STL).

kmcnamara
Download Presentation

Templates in C++: Introduction and Usage

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. C++ Lecture 9 Tuesday, 26 Aug 2005

  2. Templates • Template can be a class or function that has data types as parameters. The types are realized when the template is used, where the types are replaced by actual types. The corresponding code is generated by compiler.

  3. Overloaded Square Functions • int sq(int i) { return i*i; } • float sq(float f) { return f*f; } • double sq(double d) { return d*d; } • complex sq(complex c) { return c*c; }

  4. Template Functions • Using template, all four functions can be coded in one: template <class T> T sq(T x) { return x*x;} • Use the template function as • sq(i); sq(f); sq(d); or sq(c); Assuming i is int, f is float, d is double, and c is complex. C.f. sq.cpp

  5. General Form of Function Templates template <class T, class S, …> void func(const T *a, S b, …) { (function definition using T & S etc) } • T and S must appear in the parameter list. C.f. Fig.12.2 (old)

  6. Class Templates template <class T> class Stack { public: … bool pop(T&); private: T *stackPtr; } C.f. Fig.12.3 (old) and tstack1.h

  7. Definition of Class Functions with Template template <class T> bool Stack<T>::pop(T &popv) { …. }

  8. Use of Templates Stack<double> dstk(100); Stack<int> istk(1000); Stack<book> bstk(500); • The parameter T is instantiated by the actual type in the angular bracket < type >.

  9. Non-Type Parameters in Class Templates template<class T, int el> class Stack { … sPtr = new T[el]; … } • Stack<double, 100> stk;

  10. Standard Template Library • Container classes: vector, list, set, stack, queue etc. • Iterators: pointer-like objects • Generic algorithms: copy, fill, remove, rotate, etc. C.f. Fig 20.14 and Fig.20.15 (old)

  11. Error Handing with assert( ) • #include <assert.h> • assert(int_expr); • Program stops and prints the assert( ) code line if int_expr becomes false (I.e. int_expr is 0).

  12. Exception Handing try { code that may evoke: throw e; } catch (type e) { exception handing code } e can be a class C.f. Fig.13.1 (old)

  13. Typedef • typedef data-type new-name struct Card { int pip; char *suit; }; typedef card* cptr; cptr deck;

  14. Bitwise Operations • & bitwise AND • | bitwise OR • ^ bitwise exclusive OR • << left shift • >> right shift • ~ one’s complement

  15. Bitwise Examples • 00..0110 & 00..1100 -> 00..0100 • 00..0110 | 00..1100 -> 00..1110 • 00..0110 ^ 00..1100 -> 00..1010 • 00..0110 << 2 -> 0?..011000 • 00..0110 >> 2 -> 000..??01 • ~00..0110 -> 11..1001

  16. Command Line Arguments int main(int argc, char *argv[ ]) { … int i = atoi(argv[1]); double f = atof(argv[2]); … } • argv[0] is the string of the name of evoking program. C.f. argv.cpp (old)

  17. Summary of OOP • Information hiding and encapsulation with classes • Building new class from old (inheritance and hierarchy) • Polymorphism by function or operator overloading, and virtual functions.

More Related