Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 TEMPLATE

2 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.

3 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

4 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

5 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.

6 Generic function  Declaration syntax for generic function : template function_name( ) { //body function } Keyword template Keyword class DataType Function Name

7 #include template 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.

8 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 Example (Cont..)

9 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.

10 The Flow Of The Use Of Template In Program Example (Area Of Rectangle) In Integer And Double Value.

11 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 template 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.1 2 is bigger than 0.11 10 is bigger than 0.99

12 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. template 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 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

13 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.

14 #include template 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

15 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 Cont..

16 Explaination of Program 1.2 When 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.

17 template 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.

18 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


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

Similar presentations


Ads by Google