Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates Class Templates

Similar presentations


Presentation on theme: "Templates Class Templates"— Presentation transcript:

1 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;

2 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 main() function of the previous slide Non-type parameters in class templates 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

3 Templates 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

4 Templates Templates and friend functions and classes
friend functions of a template class template <class t> class X{ friend void f2(X<t>&); // declares f2() as friend to X<t> type objects }; X<float> a; // assumes the existance of the function f2(X<float>&), this //function can have access to all members of X<float> type objects Friend classes template <class type> class ListNode{ Type Item; ListNode<type>* Next; template <class T> friend class List; // all classes of List<T> are // friends of class ListNode<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 ListNode<type>

5 Templates Templates, Container Classes, and Class Libraries
A container class id 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 \classlib\Include have several header files which defines container class templates, these can be used as follows, #include <classlib\stacks.h> #include <iostream.h> void main(){ TStackAsVector<int> x(100); x.push(50); x.push(30); while(!x.IsEmpty()) cout << x.pop() << endl;}

6 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 = , 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)


Download ppt "Templates Class Templates"

Similar presentations


Ads by Google