Templates Class Templates Used to specify generic class

  • Slides: 6
Download presentation
Templates Class Templates • Used to specify generic class types where class members data

Templates Class Templates • Used to specify generic class types where class members data types can be specified as parameters, e. g. here is a generic List class template, template <class t> // t is a generic data type parameter class List { int size; t* list; // a pointer to an object of the generic type t public: // example member functions List(int sz = 10); t& operator[](int); }; // member functions bodies must be preceded by the template keyword template <class t> List<t>: : List(int sz){ list = new t [size = sz]; } template <class t> t& List<t>: : operator [](int j){return list[j]; } main(){ List<char> ch_arr(80); List<double> dbl_flt(15); List<Complex> x; // instantiate 3 classes and three objects

Templates Class Templates (cont. ) • Template class instantiation List<type> Object_name(parameters); // instantiates a

Templates Class Templates (cont. ) • Template class instantiation List<type> Object_name(parameters); // instantiates a List<char> // class at compile time, the generic type t is replaced by type, then the // object is instantiated as shown in the above main() function • Non-type parameters in class template <class t, int size> List { t list[size]; // statically allocated list of type t elements // the size of the list and the type of elements are determined at the class // instantiation. In the above, the size of the list is specified at compile // time no need for dynamic memory allocation, this avoids run time //errors due to new and eliminates execution overhead for memory //allocation List<float, 500> b; // instantiates the class List with float type // members and a size of 500 and instantiates an object b of this type

Templates and inheritance • An explicit Base class instantiated from a class template, e.

Templates and inheritance • An explicit Base class instantiated from a class template, e. g. , class Integer_Stack: public List<int> {//………}; // the base class is // instantiated from a the template List <class t> class • A template derived class with a non-template base class template <class t> class D: public B {// data members, member function // arguments, return types in D can take the generic type t, inherited members // of B all have explicit types • Both, a template derived class with a template base class template <class t> class Stack: public List<t> { public: Stack(int sz): List<t>(sz){} //references with formal parameters }; // the following statement instantiates class Stack<float> and class List<float> Stack<float> a(100); // object a is instantiated from these classes

Templates and friend functions and classes • friend functions of a template class template

Templates and friend functions and classes • friend functions of a template class template <class t> class X{ friend void f 2(X<t>&); // declares f 2() as friend to X<t> type objects }; X<float> a; // assumes the existance of the function f 2(X<float>&), this //function can have access to all members of X<float> type objects • Friend classes template <class type> class List. Node{ type Item; List. Node<type>* Next; template <class T> friend class List; // all classes of List<T> are // friends of class List. Node<type>, this is dangerous since type might be // different from T, it is better to do the following instead, friend class List<type> // only the List<type> class where T is replaced by type will be friend of all classes of the template class List. Node<type>

Templates, Container Classes, and Class Libraries • A container class is one that operates

Templates, Container Classes, and Class Libraries • A container class is one that operates on a collection of one or more objects of a particular type, e. g. , Lists, Stacks, Linked Lists, Queues, etc. , • Container classes are supported in the compiler’s class library as class templates, e. g. in Borland C++ the directory classlibInclude have several header files which defines container class templates, these can be used as follows, #include <classlibstacks. h> #include <iostream. h> void main(){ TStack. As. Vector<int> x(100); x. push(50); x. push(30); while(!x. Is. Empty()) cout << x. pop() << endl; }

Templates Function templates • A function template is used to specify generic functions in

Templates Function templates • A function template is used to specify generic functions in the same way as class templates is used to specify generic classes, for example, template <class T> T abs(T n){ // a generic function which returns the absolute value of object n // of type T return (n < 0) ? -n : n; } void main(){ int x = -500; long y = -100000, double z = -1. 87; cout << abs(x) << “t” << abs(y) << “t” << abs(z); // three different overloaded functions named abs are generated at compile // time, these are abs(int), abs(long), and abs(double)