template n.
Skip this Video
Loading SlideShow in 5 Seconds..
TEMPLATE PowerPoint Presentation
play fullscreen
1 / 18
Download Presentation

TEMPLATE - PowerPoint PPT Presentation

rusti
233 Views
Download Presentation

TEMPLATE

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
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. Kek coklat Chocolate cake Boleh digunakan untuk membuat Can be used to make Acuan kek Cake mould Kek Buah Fruit cake Example of template in real world.

  4. 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. Template 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 <class DataType> function_name( ) { //body function } DataType Keyword template Function Name Keyword class

  7. Example • 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

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

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

  12. 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 Hold integer data Hold double data void comparison( compare1 2, compare2 0.11) { : : } void comparison( compare1 0.99, compare2 10) { : : } Hold integer data Hold double data 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.

  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 <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); Explicit overloaded generic type for integer PROGRAM 1.2

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

  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