Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 // 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 List ::List(int sz){ list = new t [size = sz];} template t& List ::operator [](int j){return list[j];} main(){ List ch_arr(80); List dbl_flt(15); List x; // instantiate 3 classes and three objects

2 Templates Class Templates (cont.) Template class instantiation List Object_name(parameters); // instantiates a List // 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 templates template 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 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 {//………}; // the base class is // instantiated from a the template List class A template derived class with a non-template base class template 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 Stack: public List { public: Stack(int sz): List (sz){} //references with formal parameters }; // the following statement instantiates class Stack and class List Stack 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 X{ friend void f2(X &); // declares f2() as friend to X type objects }; X a; // assumes the existance of the function f2(X &), this //function can have access to all members of X type objects Friend classes template class ListNode{ type Item; ListNode * Next; template friend class List; // all classes of List are // friends of class ListNode, this is dangerous since type might be // different from T, it is better to do the following instead, friend class List // only the List class where T is replaced by type will be friend of all classes of the template class ListNode

5 Templates 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 \classlib\Include have several header files which defines container class templates, these can be used as follows, #include void main(){ TStackAsVector 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 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)


Download ppt "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."

Similar presentations


Ads by Google