{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

09templa - Templates LOOKOUT library includes several...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Templates L OOKOUT library includes several different array classes: IntArray, FloatArray, StringArray... C obiously these have a lot in common; the only difference between them is element type C Wouldn = t it be simpler if there were just one generic array class? C Enter C++ templates , a language mechanism which can generate many different functions and classes from a generic framework + particular parameters such as element types C Inspired by generic packages of Ada, generic classes of Eiffel and generic ADTs C++ has two kinds of templates: function (and operator) generators and class generators What about Java? Alas, Java has no templates or generic classes. The Java Foundation Library of JDK 1.2 provides a set of general collections classes; and ObjectSpace provides JGL (see my web page), patterned after the STL of C++ but these rely on inheritance from a root Object class, which loses type-safety and efficiency A function template can generate a whole family of functions. For example: template <class T> T max(const T &a, const T &b) { if (a > b) return a; else return b; } From this single definition a C++ compiler can generate functions called max() for any types that already support the > operator, as needed. C The keyword template introduces a generic definition. C Next comes the formal parameters of the template, between angle brackets: <class T> declares a type parameter named T . Wherever T appears, the template will use the instantiated actual parameter C in this example, T appears as the return type and two parameters types of the function C for example, suppose the following code fragment appears in the same program as max() : int x,y; //user-supplied values cin >> x >> y; cout << "max value is " << max(x,y) << endl; The compiler will automatically generate the code for a function max() that would look like this: int max(const int &a, const int &b) { if (a > b) return a; else return b; } Simirlarly, if x and y were string s (from Standard Library). Then the compiler would generate: string max(const string &a, const string &b) { if (a > b) return a; else return b; } Pretty handy! Likewise, class templates generate a family of classes, at compile-time: 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
template <class T, int SIZE> class Array { //array contains SIZE elements of type T public: Array(); //default constructor Array(const T& init); //initialize elements with init Array(const Array<T,SIZE> &other); //copy constructor int size() const; //current size or capacity int length() const; //synonym of size() ~Array(); //destructor cleans up memory T& operator[](int); //operator returns float element private: T element[SIZE]; //a block of SIZE elements of type T int sz; //size or capacity } ; Template class Array has two parameters: - the first is a class type parameter, specifying a type for the elements of the array; - the second a function style parameter, specifying an int value for the size of the array.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}