logistics
play

Logistics Project Templates Part 1 (clock and design) due Sunday, - PDF document

Logistics Project Templates Part 1 (clock and design) due Sunday, Sept 25 th Start thinking about partners for Parts 2-3 Questions? Logistics Plan for today Important date: Introduction to Templates THURSDAY is


  1. Logistics • Project Templates – Part 1 (clock and design) due Sunday, Sept 25 th – Start thinking about partners for Parts 2-3 • Questions? Logistics Plan for today • Important date: • Introduction to Templates – THURSDAY is Exam 1 – Will cover: • C++ environment / architecture • C++ variables, pointers, references • Aggregates (Arrays, struct) static, const – Will not cover • Classes • Operator overloading • Constructors, Destructors, operator= • Templates A quick intro to Templates A quick intro to Templates • Problem: • Problem: – Let’s say that we need a Queue class that manages a Queue – Now, let’s say that we need a Queue class that manages of ints: a Queue of double: class intQueue class doubleQueue { { private: private: int *q; double *q; int n; int n; … … public: public: void enqueue (int i); void enqueue (double i); int dequeue(); double dequeue(); … … } } 1

  2. A quick intro to Templates A quick intro to Templates • One C++ implementation • Note that the code for intQueue will be class voidQueue almost identical to that of doubleQueue. { private: void **q; • Is there a way to reuse the basic Queue code int n; but have it work on different datatypes? … public: void enqueue (void *i); void * dequeue(); … } A quick intro to Templates A quick intro to Templates • Problems with this approach • The template solution: – Define a “generic” queue class int *i = new int (9); // You must use pointers – Datatype of the objects managed by the queue float *f = new float (5.7); is defined later voidQueue Q; – Can define such a class using Templates: Q.enqueue (i); Q.enqueue (f); // Multiple datatypes in same queue int *g = (int *)(Q.dequeue);// must cast, is this // an int * or a float * A quick intro to Templates A quick intro to Templates template <class T> Datatype to be • To use this template: class Queue filled in later { // a queue of ints private: Queue<int> iqueue; T *q; int n; // a queue of doubles … Queue<double> dqueue; public: void enqueue (T i); // a queue of aClass objects T dequeue(); Queue<aClass> aqueue … } // a queue of pointers to aClass Queue<aClass *> aptrqueue 2

  3. Template Example Template Example template < class T > • Safe array class SafeArray { – Looks and feels like an array public: SafeArray( int initSize ); – Will be responsible for it’s own memory ~SafeArray(); management T &operator[]( int index ); – Will fail if you try to access beyond the bounds int getSize(); of the array private: – Can hold any datatype or object. T *data; // an array int size; } Template Example Template Example // constructor • operator[] returns a reference to T template < class T > // operator[] SafeArray< T >::SafeArray( int initSize ): // data( 0 ), size( initSize ) { template < class T > assert (initsize > 0) // punt if initsize !> 0 T &SafeArray< T >::operator[]( int index ) data = new T[ size ]; { } assert( ( index >= 0 ) && ( index < size ) ); // destructor return data[ index ]; template < class T > SafeArray< T >::~SafeArray() } { delete[] data; } Template Example Alternate implementation of operator[] int N = 5; template < class T > T &SafeArray< T >::operator[]( int index ) SafeArray< int > i_good( N ); { SafeArray< std::string > s_good( N ); if (index >= size) { // create a new array capable of holding the indexed element s_good[0] = "bridge"; T *new_data (new T[ index + 1 ]); s_good[1] = "to"; // copy the data over from the old to new array s_good[2] = "the"; for (int i( 0 ); i < size; i++) { new_data[ i ] = data[ i ]; } s_good[3] = "21st"; // delete the old array s_good[4] = "century"; delete[] data; // point data to the new array for ( int x = 0 ; x < N; x += 1 ) data = new_data; // increase the size count { size = index + 1; i_good[ x ] = 2 * x; } } return data[ index ]; } i_good[5] = 24; // Will cause assertion to fail. 3

  4. Template arguments Template arguments • Template arguments can be of ordinary types: • Template arguments can be of ordinary template < class T, int S > types: class SafeArray { public: // constructor SafeArray(); template < class T, int S > ~SafeArray(); SafeArray< T, S >::SafeArray( ): data( 0 ), size( S ) T &operator[]( int index ); { int getSize(); private: assert (S > 0) T *data; // an array data = new T[ size ]; int size; } } Template arguments Template arguments • Template arguments can be of ordinary types: • Template arguments can have defaults template < class T, int S=10> class SafeArray { SafeArray< int, 5 > i_good; public: SafeArray(); for ( int x = 0 ; x < N; x += 1 ) ~SafeArray(); { i_good[ x ] = 2 * x; T &operator[]( int index ); } int getSize(); private: i_good[5] = 24; // Will cause assertion to fail. T *data; // an array int size; } Template arguments Template arguments SafeArray< int > i_good; • Template arguments can be given in terms of other template arguments template < class T, T default_val > Will be an int smart array of size 10 class SafeArray { public: SafeArray(int initize); ~SafeArray(); T &operator[]( int index ); int getSize(); private: T *data; // an array int size; } 4

  5. Template arguments Template arguments • Template arguments can be given in terms of other • Template arguments can be given in terms template arguments of other template arguments // constructor template < class T, T default_val > SafeArray<float, 3.4> farray; SafeArray< T, default_val >::SafeArray( int initSize ): SafeArray<int, 12> iarray; data( 0 ), size( initsize ) SafeArray<string, “foo”> sarray; { assert (size > 0) data = new T[ size ]; for (int i=0; i < size; i++) data[i] = default_val; } Class Templates Function Templates • Questions? • Functions can also be templated – Data type of function arguments and return type filled in later. – Good for “common” algorithms • Search • Sort • Generic functions Function Templates Function Templates • Use a template function like a regular function // max – No need to specify type explicitly // returns the maximum of the two void main() elements { template <class T> cout << "max(10, 15) = " << max(10, 15) << endl ; cout << "max('k', 's') = " << max('k', 's') << endl ; T max(T a, T b) cout << "max(10.1, 15.2) = " << max(10.1, 15.2) << endl ; { } return a > b ? a : b ; Program Output } max(10, 15) = 15 max('k', 's') = s max(10.1, 15.2) = 15.2 5

  6. Function Templates Templates • However, object passed to max must define the > • Note that compiler will actually generate operation. functions/classes for each instantiated templated class or function it encounters. main (int argc, char *argv[]) { • When the compiler generates a class, function or Foo f1 (1,2); static data members from a template, it is referred Foo f2 (2,3); Foo f3 (4,4); to as template instantiation. f3 = mymax (f1, f2); } – A class generated from a class template is called a generated class. "foo.cpp", line 10: Error: The operation "Foo > Foo" is illegal. "foo.cpp", line 25: Where: While instantiating "mymax<Foo>(Foo, – A function generated from a function template is called Foo)". a generated function. "foo.cpp", line 25: Where: Instantiated from non-template code. 1 Error(s) detected. Templates Summary • Templates • Templates get resolved at compile-time. – Generic Programming – Unlike polymorphism that gets resolved at – Datatypes filled in later runtime. – Class Templates – Function Templates • Questions? Next time • The standard template library (STL) • Exam 1 6

Recommend


More recommend