170 likes | 281 Vues
In this lecture on C++, we explore templates as powerful tools that allow both classes and functions to use data types as parameters. We demonstrate how to implement template functions, enabling code reuse for different types, with examples such as overloaded square functions. The lecture also covers non-type parameters in class templates, standard template library containers, and basic error handling using assertions. Finally, we summarize object-oriented programming principles, including encapsulation, inheritance, and polymorphism, enhancing your understanding of C++ programming.
E N D
C++ Lecture 9 Tuesday, 26 Aug 2005
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.
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; }
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
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)
Class Templates template <class T> class Stack { public: … bool pop(T&); private: T *stackPtr; } C.f. Fig.12.3 (old) and tstack1.h
Definition of Class Functions with Template template <class T> bool Stack<T>::pop(T &popv) { …. }
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 >.
Non-Type Parameters in Class Templates template<class T, int el> class Stack { … sPtr = new T[el]; … } • Stack<double, 100> stk;
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)
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).
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)
Typedef • typedef data-type new-name struct Card { int pip; char *suit; }; typedef card* cptr; cptr deck;
Bitwise Operations • & bitwise AND • | bitwise OR • ^ bitwise exclusive OR • << left shift • >> right shift • ~ one’s complement
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
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)
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.