TEMPLATE
Introduction
Template can be assumed as one that can be used to develop a function or class.
C++ use template to achieve polymorphism that is during compilation.
One example of template in real world is a cake mould. A cake mould can be used for making a chocolate or fruit cake.
By using template, it allows programmer create generic function and generic class function.
Example of template in real world.
Acuan kek
Cake mould
Boleh digunakan untuk membuat
Can be used to make
Kek coklat
Chocolate cake
Kek Buah
Fruit cake
Template
Is a frame or a form that is use to generate a function or a class.
One of the way to achieve polymorphism
Allows the programmer to generate generic function and generic class.
Generic Function: Generic function is a draft for a function that can be used to generate a few same functions in different version.
Advantage: Codes are shorter and easier to understand
Generic function
Generic function defines a general set of operations that can be used on various types of data.
Generic function is created using the keyword template, followed by a formal template parameter list, which is enclosed within angle bracket (< >). Each parameter represents data types must be began with class keyword. After that, function name for generic function will be defined.
Generic function
Declaration syntax for generic function :
template <class DataType> function_name( ){//body function}
Keyword template
Keyword class
DataType
Function Name
#include <iostream.h> template <class rect> void area(rect length, rect width){ rect r; r= length * width; cout<<r<<'\n';}void main(){ int i=10,j=20; double y=10.1,z=4.2; cout<<"Length (in integer value): "<<i<<'\n'; cout<<"Width (in integer value): "<<j<<'\n'; cout<<"Area of rectangle in integer value: "; area (i,j); cout<<"Length (in double value): "<<y<<'\n'; cout<<"Width (in double value): "<<z<<'\n'; cout<<"Area of rectangle in double value: "; area (y,z); }
PROGRAM 1.0
Example implement the
generic function concept to calculate area of a rectangle in integer and double values.
Output Program 1.0Length (in integer value): 10Width (in integer value): 20Area of rectangle in integer value: 200Length (in double value): 10.1Width (in double value): 4.2Area of rectangle in double value: 42.42
Example (Cont..)
Explaination of Program 1.0In this example when generic function area(i, j)is called. Integer value hold by i and j will be used to calculate area of rectangle in integer value. When generic function the called on area(y,z), which y and z hold double value. So, when y and z value send to generic function, this value will be used to calculate area of rectangle in double value.
The Flow Of The Use Of Template In Program Example (Area Of Rectangle) In Integer And Double Value.
Function with two generic function You can define more than one generic data type in a template
statement by using coma (,) to separate generic data types. The program 1.1 below is used to compare 2 values.
#include <iostream.h>
template<class compare1, class compare2>
void comparison( compare1 x, compare2 y){ if (x>y)
cout<<x<<" is bigger than "<<y<<'\n'; else
cout<<y<<" is bigger than "<<x<<'\n';} void main(){comparison(2,0.11);comparison(0.99,10);}
2 types of generic data
Output Program 1.12 is bigger than 0.1110 is bigger than
0.99
Explaination of Program 1.1In program 1.1, compare1 holds integer value and compare2 holds double value when comparison(2,0.11) called in main(). But when comparison(0.99,10) called in main(), compare1 will hold double value compare2 will hold integer value.
template<class compare1, class compare2>
void comparison( compare1 x, compare2 y)
{ :
}
comparison(2,0.11); comparison(0.99,10);
When value 2 and 0.11 are send to generic function
When value 2 and 0.11 are send to generic function
void comparison( compare1 2, compare2 0.11)
{
:
:
}
Hold integer data
Hold double data
void comparison( compare1 0.99, compare2 10)
{
:
:
}
Hold integer data
Hold double data
Explicitly overloading generic types
When you call generic function, argument of function will determine types of data that will be used in function.
However, C++ allows you to do pre-definition data types by determining types of data that you want program to manipulate.
The program 1.2 is used to calculate the area of a rectangle by using specific generic type conditions for integer type data.
#include <iostream.h>template <class rect> void area(rect length, rect width){ rect r; r= length * width; cout<<r<<'\n';}void area(int length, int width){ int r; r= length * width; cout<< "Area of rectangle in integer value: "<<r<<'\n';} void main(){ int i=10,j=20; double y=10.1,z=4.2; cout<<"Length (in integer value): "<<i<<'\n';cout<<"Width (in integer value): "<<j<<'\n'; area (i,j);
PROGRAM 1.2
Explicit overloaded generic type for integer
cout<<"Length (in double value): "<<y<<'\n'; cout<<"Width (in double value): "<<z<<'\n'; cout<<"Area of rectangle in double value: "; area (y,z); }
Output Program 1.2Length (in integer value): 10Width (in integer value): 20Area of rectangle in integer value: 200Length (in double value): 10.1Width (in double value): 4.2Area of rectangle in double value: 42.42
PROGRAM 1.2 Cont..
Explaination of Program 1.2When area(i,j) called, it will call area() that has pre-defined data types which is area(int length, int width) because this function has defined integer data type. Since i and j value are an integer, function area() that has pre-define will be called. However for area(y,z), it will called function area() that has no data type pre-define because there is no pre-define data types function for double value.
template <class rect>
void area(rect length, rect width)
{
:
}
area(i,j) area(y,z)
Value of integer i and j are send to generic function which has pre-defined data type integer
Value of integer y and z are send to general generic function
void area(int i, int j)
{
:
}
void area(rect y, rect z)
{
:
}
Template usage flow diagram in this example shows clearly rectangle area that consists.
Generic function limitation Generic function is almost like an overloaded
function except it has more limitation. Generic function must do the same action for all
version - only data types can be different. The program 1.3 shows error when outdata()
function do not do the same thing.void outdata (int i){
cout << i;} void outdata(double d){
cout << d*3.1416;}
PROGRAM 1.3