Download presentation
Presentation is loading. Please wait.
1
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates
2
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 2 Introduction - Template enables us to specify with a single code segment, an entire range of related functions called template functions or an entire range of related classes called template classes - For example, we can write a single function template for an array that - sorts an array of integers - sorts an array of doubles - or even an array of a complex type such as employee class
3
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 3 Function Template - Suppose we want to write a function that prints the contents of an array. - If this array is an array of integers, a possible implementation can be as follows: void printArray (int array[], int arraySize) { for (int i=0; i<arraySize; i++) cout << array[i] << “ ”; } - where array is the name of the array and arraySize refers to the size of the array.
4
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 4 - Note that the prototype of this function can be written as: void printArray(int[], int); - Can we call the above function to print other types of arrays such as an array of double, or an an array of char, or an array of class employee, and so on? - The answer is that with Traditional C language, this is not possible. However, with object-oriented tools it is possible.
5
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 5 - In traditional C, we need to take care of two issues: - First, we need to have a separate print function, one for each type of the array we use in our program - Second, if all of these arrays are under one project (program) their names must be different. - we cannot have two function of the same name in Traditional C language - For example, if we have three arrays of types int, char, double, we may need to have three functions such as: void printIntArray(int[], int); void printDoubleArray(double[], int); void printCharArray(char[], int);
6
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 6 Overloading the functions - Remember the concept of overloading in C++. - In C++, we can overload the functions. This means, we can have several functions with the same name but different signature (their return type and/or parameters have different types, or number of parameters can be different). - If we overload our printArray function we can have three functions one for each different type but all with the same name. void printArray(int[], int); void printArray(double[], int); void printArray(char[], int);
7
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 7 Using function Template - With overloading, we do not need to give each function a distinct name; however, we still need to have one function for each type of the array. - If we use a template function, to print the array, we only need to write one function to take care of any form of array. - Based on the argument types provided explicitly or inferred from calls to the template function, the compiler generates separate object-code functions to handle each type of call appropriately.
8
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 8 Syntax - for our printArray function, the syntax is as follows: template void printArray (T array[], int arraySize) { for (int i=0; i<arraySize; i++) cout << array[i] << “ ”; } - All function template definitions begin with the keyword template followed by a list of formal type parameters to the function template enclosed in angle bracket ( ) - Each formal type parameter must be proceeded by the keyword class or a typename
9
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 9 -You can have more than one array to be printed template void printArray(const T1 array1[], int count1, const T2 array2[], int count2) { for (int i=0; i<count1; i++) cout << array1[i] << " "; cout << endl; for (i=0; i<count2; i++) cout << array2[i] << " "; cout << endl; } void main() { const int aCount =5, cCount = 12; int a[aCount] = {1, 2, 3, 4, 5}; char c[cCount] = "HELLO THERE"; printArray(a, aCount, c, cCount); }
10
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 10 Class Template -Class template are called parameterized types because they require one or more type parameter to specify how to customize a “generic class” template to form a specific template class -As an example, remember about the stack data structure with the first in last out feature -Stack can be of any data type. We can have a stack of integers, a stack of doubles, a stack of Employee class or stack of Student class. -The following is a typical declaration of a stack class including the implementation of the methods
11
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 11 class Stack { public: Stack(); bool push(const int&); bool pop(int&); private: int size; int top; int stk[3]; }; Stack::Stack() { size = 3; top = -1; } bool Stack::push(const int& pushValue) { if (top+1 < size) { top++; stk[top] = pushValue; return true; } return false; } bool Stack::pop(int& popValue) { if (top>=0) { popValue = stk[top]; top--; return true; } return false; }
12
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 12 What is the limitations of our Stack class? -One of the limitation of our stack class is that all objects of the type Stack class must be integer stack. -What about objects of type stack of doubles or stack of chars, or even more complex classes such as stack of Employees or stack of Students? -One solution is to create one stack class for each desirable type. For example, one for stack of char, another for stack of int, another for stack of double and so on. - This is not a feasible solution because we need to create a new class every time we need a stack of a new type.
13
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 13 Using template class -Another solution is to use template class. - If we make the stack to be a class template, our stack class become the basis for creating many stacks classes such as stacks of int, stacks of double, and so on. -The next slide shows how to modify our Stack class to become a class template for any type of Stack class.
14
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 14 template class Stack { public: Stack(); bool push(const T&); bool pop(T&); private: int size; int top; T stk[3]; }; template Stack ::Stack() { size = 3; top = -1; } template bool Stack ::push(const T& pushValue) { if (top+1 < size) { top++; stk[top] = pushValue; return true; } return false; } template bool Stack ::pop(T& popValue) { if (top>=0) { popValue = stk[top]; top--; return true; } return false; }
15
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 15 -In general to produce a variety of template classes, we simply need to write one class template definition. -Each time we need a new type specific instantiation, we can declare the associated object and the compiler writes the source-code for the template class we require. -In the main program, we now can use the same class to create two different types of stacks void main() { Stack doubleStack; Stack intStack; …. }
16
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 16 - Note that each member function definition outside the class begins with the header template - The binary scope resolution operator is used with the class template name Stack to tie the member function definition to the class template's scope
17
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 17 - One of the limitations of the previous example is that all stacks have one size, size 3 - We can slightly change our implementation to be able to instantiate stacks of different sizes with different types - In the previous example, the stack class template section used only one type parameter in the template header - It is also possible to use non-type parameters. - For example, the template header in the previous example can be modified to take a non-type parameter as follows: template
18
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 18 - Then a declaration such as Stack myStack would instantiate a stack of double with the capacity of 100 elements. - the complete class header and the implementations of the methods are shown in the next slide
19
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 19 template class Stack {public: Stack(); bool push(const T&); bool pop(T&); private: int size; int top; T stk[elements]; }; template Stack ::Stack() { size = elements; top = -1; } template bool Stack ::push(const T& pushValue) { if (top+1 < size) {top++; stk[top] = pushValue; return true; } return false; } template bool Stack ::pop(T& popValue) { if (top>=0) { popValue = stk[top]; top--; return true; } return false; }
20
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 20 template class Node { public: T object; Node *next; }; template class LL { private: Node *top; public: LL(); ~LL(); void insertLL(T); void printLL(T); }; template LL ::~LL() { Node *curr = top, *p; while (curr!= NULL) { p = curr; curr = curr->next; delete p; } top = NULL; } template LL ::LL() { top = NULL; } template void LL ::insertLL (T value) { Node *NewNode; NewNode = new (Node ); NewNode->next = top; NewNode->object = value; top = NewNode; } template void LL ::printLL() { Node *curr; cout << endl; curr = top; while (curr != NULL) { cout object "; curr = curr->next; } cout << endl; }
21
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 21 int main () { LL list1; list1.insertLL(10); list1.insertLL(20); list1.insertLL(30); cout << "--------------------------------" << endl; list1.printLL(); LL list2; list2.insertLL("test"); list2.insertLL("a"); list2.insertLL("is"); list2.insertLL("This"); cout << "--------------------------------" << endl; list2.printLL(); return 0; }
22
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 22 Class template and Template Classes - Template classes of a class template are the set of classes generated at run time for that class template - For example, in our example, for the class template stack, we may create three different objects like: Stack doubleStack Stack intStack Stack charStack - Instantiation of objects doubleStack, charStack, and intStack causes the following classes be generated:
23
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 23 class Stack {public: Stack(); bool push(const int&); bool pop(int &); private: int size; int top; int stk[20]; }; class Stack {public: Stack(); bool push(const char&); bool pop(char &); private: int size; int top; char stk[30]; }; class Stack {public: Stack(); bool push(const double&); bool pop(double &); private: int size; int top; double stk[10]; };
24
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 24 Templates and Inheritance - A class template can be derived from a template class - A class template can be derived from a non-template class - A template class can be derived from a class template - A non-template class can be derived from a class template
25
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 25 Template and friends function - Inside a class template for class X that has been declared with template class X - a friend declaration of the form friend void f1(); makes the stand-alone function f1() a friend of every template class instantiated from the preceding class template - a friend declaration of the form friend void A::f1(); makes method f1() of class A a friend of every template class instantiated from the preceding class template - a friend declaration of the form friend class Y makes every method of class Y a friend of every template class instantiated from the preceding class template
26
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 26 Template and friends function - Inside a class template for class X that has been declared with template class X - a friend declaration of the form friend void f2(X &); for a particular type T such as float makes function f2(X &) a friend of X only - Or a friendship declaration of the form friend class Z when a template class is instantiated with a particular type for T such as float, all members of class Z become friends of template class X.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.