 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
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.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Structures Spring 2013Programming and Data Structure1.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2006 Pearson Education, Inc. All rights reserved Templates.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Function Part II: Some ‘advanced’ concepts on functions.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
1 CSC241: Object Oriented Programming Lecture No 27.
Templates Zhen Jiang West Chester University
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 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Learners Support Publications Classes and Objects.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Learners Support Publications Functions in C++
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.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
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.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
C++ Templates.
Chapter 14 Templates C++ How to Program, 8/e
CS212: Object Oriented Analysis and Design
Introduction to Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
More ‘concepts’ on Function
Classes and Objects.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
CS2011 Introduction to Programming I Methods (II)
Exception Handling.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
More ‘concepts’ on Function
Corresponds with Chapter 5
Presentation transcript:

 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures. void swap( int &a, int &b) { int temp; temp=a; a=b; b=temp; } void swap( float &a, float &b) { float temp; temp=a; a=b; b=temp; }

 A template can be created with some or all variables therein having unspecified data types.  Whenever the template is invoked by passing arguments of a certain type, the C++ language on its own replaces the unspecified type with the type of the arguments passed. Syntax for creating a template for a generic function: template return_type function_name (T arg1,..) { // statements }

#include class student { char name[10]; int age; public: student(char a[]=0, int b=0) { strcpy(name,a); age=b; }

void printdata() { cout<<endl<<"Name:"<<name; cout<<endl<<"Age:"<<age; } }; template void swap( T &a, T &b) { T temp; temp=a; a=b; b=temp; }

void main() { clrscr(); int x,y; x=10; y=20; cout<<endl<<" BEFORE SWAPPING INTEGERS:"<<x<<" " <<y; swap(x,y); cout<<endl<<" AFTER SWAPPING INTEGERS:"<<x<<" " <<y; float a,b; a=1.1; b=2.2; cout<<endl<<" BEFORE SWAPPING FLOATS:"<<a<<" " <<b; swap(a,b);

cout<<endl<<" AFTER SWAPPING FLOATS:"<<a<<" " <<b; student s1("aaa",12); student s2("bbb",21); cout<<endl<<" BEFORE SWAPPING OBJECTS:"; s1.printdata(); s2.printdata(); swap(s1,s2); cout<<endl<<"AFTER SWAPPING OBJECTS:"; s1.printdata(); s2.printdata(); getch(); }

 The compiler generates the actual function from a template only once for a given data type.  The compiler first looks for an exact match to resolve the function call before looking for a template.  If it finds an exact match, It does not look for the template.  The entire definition of the function must appear in the header file.  The ‘larger’ template will work for int and float. When used for string, does not work correctly. It should be overloaded with the special version.  FUNCTION TEMPLATES CAN BE OVERLOADED.

#include template T& larger( T& a, T& b) { return a>b? a:b; } char* larger( char* a,char* b) //overloading { return strcmp(a,b)>0?a:b; }

void main() { clrscr(); int a=10; int b=20; cout<<endl<<" The Larger number is :“ <<larger(a,b); float c=5.8; float d=3.4; cout<< endl<<" The larger float is:” <<larger(c,d); char e[5]; strcpy(e,"abc"); char * f="qwe"; char* g=larger(e,f); cout<<endl<<" The larger string is :"<<g; getch(); }

 More than one generic type can also be mentioned in the template. template void f1(const T & a, const U & b) { // statements }

#include #include #include template T& display(const T & a) {cout<<endl<<"inside single parameter function;";} template T& display(const T& a, const T & b) {cout<<endl<<" inside the 2 parameter function;"; } void main() { clrscr(); display(10); display(12,1); getch(); }

 Arises out of the need to have generic classes that handle data of different types. class x_int { int val; void f1( int &); void f2(int &); }; class x_char { char val; void f1( char &); void f2(char &); };

 The classes are similar, other than the type of their data type. template class X { T val; public: void f1( T&); void f2(T&); };

 Member functions of class templates are defined as follows: template void X ::f1(T & p) { // definition }

 The definition of the template starts with the keyword ‘template’.  The list of template arguments ( type and non-type) are enclosed in angular brackets.  Each type template argument is preceded by the keyword class.  Each non type template argument is preceded by the data type.  Member functions are defined the same way as the template class itself.  In a function definiton, The class name given before the scope resolution operator is followed by the names of all template arguments enclosed in angular brackets.

 Objects of the template class can be declared as follows X intObj;  When the compiler sees the declaration of the object, it replaces each occurrence of the template argument by the template parameter in the definition of the class template and generates a separate class.  In the preceding case, each occurence of the token T in the class X will be replaced by the keyword ‘int’.  X intObj1,intObj2; intObj1.f1(intObj2);

 The compiler generates the exact definition of a class from a given template once only for each data type and then reuses it.  A template class can take more than one template type argument. template class X {T val1;U val2; //rest of class X }  It can also take a non-type template argument template class X {T val1;//rest of class X } while declaring object of such a class, a data type will be passed as parameter for type tempalte arg.X intObj;

 The name of the template argument can not be used more than once in the list of template arg. template //ERROR  Te same name can be used in two different template classes template class X { //defn. of class X } template class Y { //defn. of class Y }  The name of the template argument need not be the same in the declaration and the definition of the template class template class X ; //declaration template class X { //defn. of class X }

 Formal arguments of the template functions can be objects of the template class template class X { //defn. of class X }; template void f1(X v) { definition of the function }  Nested classes can be created for template classes in the same way as of non-template classes. template class X { class Y { T x; //rest of class Y }; //definition of class X };

const int MAX = 10; // stack example Template class stack { T stk[MAX]; int top; public: stack() {top=-1;} void push(Tdata) { if (top==MAX-1) cout<<endl<<“stack is full”; else { top++; stk[top]=data; } } T pop() { if(top==-1) {cout<<endl<< “stack empty”; return NULL; }

else { T data = stk[top]; top--; return data; } } }; class complex { float real, imag ; public: complex(float r=0.0,float i=0.0) { real = r; imag = i; } friend ostream&operator<<(ostream&o,complex&c); }; ostream&operator<<(ostream&o,complex&c) { o<<c.real<<“\t”<<c.imag; return 0; } } main() { stack s1; s1.push(10); s1.push(20);

s1.push(30); cout s2; s2.push(3.14); s2.push( 6.28); s2.push( 9.53); cout<<endl<<s2.pop(); complex c1(1.5,2.5),c2(3.5,4.5),c3(-1.5,-2.5); stack s3 s3.push(c1); s3.push(c2); s3.push(c3); cout<<endl<<s3.pop(); }