Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
Introduction
Generic function is a draft for a function that can be used to generate a few same functions in different version. The generic class is used to generate new classes in a few different versions. The advantages of a template are that coding can be shortened and made easier. If using generic class, new classes can be without coding the definition, only by using the current generic class.
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 :
Keyword template Keyword class DataType Function Name
Generic function
See example program 1.0, implement the generic function concept to calculate area of a rectangle in integer and double values.
#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
Output Program 1.0 Length (in integer value): 10 Width (in integer value): 20 Area of rectangle in integer value: 200 Length (in double value): 10.1 Width (in double value): 4.2 Area of rectangle in double value: 42.42
Explaination of Program 1.0 In 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.
#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); }
PROGRAM 1.1
Explaination of Program 1.1 In 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.
#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 Cont..
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.2 Length (in integer value): 10 Width (in integer value): 20 Area of rectangle in integer value: 200 Length (in double value): 10.1 Width (in double value): 4.2 Area of rectangle in double value: 42.42
PROGRAM 1.2
Explaination of Program 1.2 When area(i,j) called, it will call area() that has predefined 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.
void outdata (int i) { cout << i; } void outdata(double d) { cout << d*3.1416; }
PROGRAM 1.3