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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Exercise 2.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Overview creating your own functions calling your own functions.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Function Part II: Some ‘advanced’ concepts on functions.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
CS212: Object Oriented Analysis and Design
1 CSC241: Object Oriented Programming Lecture No 25.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions + Overloading + Scope
Introducing Templates and Generics in C++
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Function Topic 4.
Chapter 14 Templates C++ How to Program, 8/e
Chapter 5 Classes.
Pointers and Pointer-Based Strings
Method Mark and Lyubo.
Extra.
CSC1201: Programming Language 2
Pointers and Pointer-Based Strings
CSC1201: Programming Language 2
Defining Classes and Methods
Functions Imran Rashid CTO at ManiWeber Technologies.
Templates CMSC 202, Version 4/02.
Introduction to Methods and Interfaces
Presentation transcript:

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

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

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

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.

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 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 is bigger than is bigger than 0.99

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

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

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: PROGRAM 1.2 Cont..

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.

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.

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