[ Pobierz całość w formacie PDF ]
.3.What is the difference between a type-specific template friend class and ageneral template friend class?The general template friend function creates one function for every type of the parameterizedclass; the type-specific function creates a type-specific instance for each instanceof the parameterized class.4.Is it possible to provide special behavior for one instance of a template butnot for other instances?Yes, create a specialized function for the particular instance.In addition to creatingArray<t>::SomeFunction(), also create Array<int>::SomeFunction()to change the behavior for integer arrays.5.How many static variables are created if you put one static member into a templateclass definition?One for each instance of the class.Exercises1.Create a template based on this List class:class List{private:public:List():head(0),tail(0),theCount(0) {}virtual ~List();void insert( int value );void append( int value );int is_present( int value ) const;int is_empty() const { return head == 0; }int count() const { return theCount; }private:class ListCell{public:ListCell(int value, ListCell *cell = 0):val(value),next(cell){}int val;ListCell *next;};ListCell *head;ListCell *tail;int theCount;};Here is one way to implement this template:template <class Type>class List{public:List():head(0),tail(0),theCount(0) { }virtual ~List();void insert( Type value );void append( Type value );int is_present( Type value ) const;int is_empty() const { return head == 0; }int count() const { return theCount; }private:class ListCell{public:ListCell(Type value, ListCell *cell = 0):val(value),next(cell){}Type val;ListCell *next;};ListCell *head;ListCell *tail;int theCount;};2.Write the implementation for the List class (non-template)version.void List::insert(int value){ListCell *pt = new ListCell( value, head );assert (pt != 0);// this line added to handle tailif ( head == 0 ) tail = pt;head = pt;theCount++;}void List::append( int value ){ListCell *pt = new ListCell( value );if ( head == 0 )head = pt;elsetail->next = pt;tail = pt;theCount++;}int List::is_present( int value ) const{if ( head == 0 ) return 0;if ( head->val == value || tail->val == value )return 1;ListCell *pt = head->next;for (; pt != tail; pt = pt->next)if ( pt->val == value )return 1;return 0;}3.Write the template version of the implementations
[ Pobierz całość w formacie PDF ]