CS60 Lecture 21 --------------- Announce ------------- - proj5 posted, due nov25th (tues) - hw14 due! This Week ------------- - Templates - Exception Handling - the STL Today -------------- - Review C++IO and Strings - Templates - proj5 Review ------------------ - C++ IO operates on 'streams' as opposed to file descriptors or FILE * - inheritance hierarchy - basic IO on ifstream/ofstream objects uising << and >> - character IO use get() and put() - whole lines use non member function 'getline' - random access use fstream type, gseek and pseek, read and write - C++ strings very nice - +, =, ==, << and >> ops all work - find(), substr(), length(), empty() - c_str() Templates ------------------------- - overloading operators is nice for the programmer, one other reason they exist: overload ops + template == generic code. - templates motivated using example ex: int findmin(int *data, int len) { int i; int min = data[0]; int ret = 0; for (i=1; i int findmin(T *data, int len) { int i; T min = data[0]; int ret = 0; for (i=1; i(myints, 4); cout << "min of myints: " << myints[idx] << endl; idx = findmin(mydoubs, 4); cout << "min of mydoubs: " << mydoubs[idx] << endl; - syntax can be confusing!! Key, as usual, is to understand what the heck is going on - we are using the word 'template' appropriately. Normal function definitions are translated directly, as written, into object code. - templates are 'descriptions' of the real code that will be generated, once actual types are decided upon. - compiler first looks to see what actual types are being passed to the template function - compiler then generates actual function code FOR EACH TYPE that it found - if, in main, we called 'findmin' with an int array, a double array, and a char array, three compiled versions of the code would be generated Template Classes ---------------------------- - in addition to functions, we can also define template classes template class node { private: T *data; node *rchild, *lchild; public: node() {data = rchild = lchild = NULL;}; node(T *in) {data = in;}; T *getdata(); }; - again this is just a placeholder for actual class code - in main int *d = new int(10); double *f = new double(10.3); node newnode(d); node newnode(f); - compiler finds that two versions of 'node' are being used, and so creates two object definitions - member functions also duplicated and must be marked as templates template T *node::getdata() { return(data); } - arg syntax again. imagine what is happening: intnode doublenode int intnode::getdata double doublenode::getdata - can be somewhat memory inefficient! duplication... Danger! Preconditions ---------------------- - must be very careful when designing template classes - back to findmin T min = data[0]; if (data[0] < min) { - remember template classes work for ANY type, including our own types - if = and < are overloaded for one of our types, we're good - but wait, destructor also has to work properly... - ooo also copy constructor if we have a function that takes parameter by value - << ... - lesson is: if you want to use a usr defnd class as a template type, it must be VERY Complete otherwise VERY BAD THINGS happen....at runtime.