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.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
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.
C/c++ 4 Yeting Ge.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
T EMPLATES Chapter 11 (11.1 and 11.2 only) Department of CSE, BUET 1.
OOP Etgar 2008 – Recitation 51 Object Oriented Programming Etgar 2008 Recitation 5.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
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.
CS212: Object Oriented Analysis and Design
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Overview of C++ Templates
CSI 3125, Preliminaries, page 1 Compiling the Program.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
CSCI 62 Data Structures Dr. Joshua Stough December 2, 2008.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Generic Programming and Library Design Brian Bartman
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
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.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
 2006 Pearson Education, Inc. All rights reserved Templates.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
MAITRAYEE MUKERJI Object Oriented Programming in C++
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Motivation for Generic Programming in C++
Introducing Templates and Generics in C++
Exceptions, Templates, and the Standard Template Library (STL)
Inheritance and Overloading
Pointers and Pointer-Based Strings
Generic programming – Function template
Templates.
Name: Rubaisha Rajpoot
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Pointers and Pointer-Based Strings
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

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 or class operates is specified as a parameter. Thus using one function or class with several different types. Inheritance provide a way to re-use object code Templates provide a way to re-use source code

3 Generic Functions A generic function defines a general set of operations that will be applied to various types of data. A single general procedure can be applied to a wide range of data. Based on the argument types provided in the function call.

4 Generic Functions compiler will automatically replace the actual data type when it creates a specific version of the function. The keyword class to specify a generic type in a template declaration template template ret-type func-name(parameter list) { // body of function }

5 Generic Functions template void swap(T a, T b) { T temp=a; a=b; b=temp; cout<<"a= "<<a<<" b= "<<b; } Swap(1,2);// Generic type is int Swap(2.5,4.6);// Generic type is float Swap(‘a’,’b’);// Generic type is char swap("popo","peepe");// Generic type is string

6 Generic Functions template T square( T one) {return(one*one);} main() { cout<<“square of integer”<<square(2);// Generic type is int cout<<“square of float”<<square(2.5);// Generic type is float }

7 Generic Functions A Function with Two Generic Types we can define more than one generic data type in the template statement by using a comma-separated list. template void myfunc(type1 x, type2 y) { cout << x << ' ' << y << '\n'; } main() { myfunc(10, "I like C++");// first is int sec is string myfunc(98.6, ‘a’);// first is float sec is char }

8 Generic Functions Function template with multiple parameters we can mix standard parameters with generic type parameters in a template function. These nongeneric parameters work just like they do with any other fun Template Void show( T x,int y ) {cout<<x<<y;} Since the first argument is a generic type, can be used to display any type of data Second argument must be integer Show(“popo”,3);// first is any type sec must be int Show(2,5);// first is any type sec must be int Show(‘A’,8);// first is any type sec must be int

9 Generic Functions Overloading of template fun Template fun can overloaded either by template funs or by ordinary funs of its name Calling ordinary fun, that has an exact match Calling template fun that could be created with exact match An error occurs if no match is found

10 Generic Functions Overloading of template fun template void show(T x){cout<<x;}// template fun void show(int x){cout<<x;}// ordinary fun main() { show(10);//o rdinary fun will call (10 is int exact match) show(2.4);//template fun will call show(‘A’);//template fun will call }

11 Generic Class Generic classes are useful when a class uses logic that can be generalized. Consider a class is created with 3 integer variables and 1 fun to find biggest among 3 ints Suppose we want to perform same above operation with float, solution is to change the data type of integers to float Using template class we can solve it to perform same operations for any type of data

12 Generic Class template class A { T a,b; public: void swap(T x, T y) { a=x; b=y; T temp=a; a=b; b=temp; cout<<"a= "<<a<<" b= "<<b; } }; void main() { A obj; obj.swap(1,2); }

13 Generic Class A obj; The object obj works like same as the of the class in which a and b are int type A obj; The object obj works like same as the of the class in which a and b are char type

14 Generic Class Class template with multiple parameters Template Class example { T1 a; T2 b; Public: example(T1 x,T2 y){ a=x;b=y;} void show(){cout<<a<<b;} main() { example obj1(1,2);// first arg is int and sec is int example obj2(4.6,2);// first arg is float and sec is int example obj2(4.6,2);// first arg is char and sec is float }

15 Template Restrictions Restrict the Types of Template Argument for a Template Class Torestrict the type of template arguments for a template class by using the Forward template declaration. Suppose a template class, want to use it only for types int : Then in object creation class object; Eg A obj; specify the type is int

16 Stack implementation using Template template class S { T stack[10]; int max,st;a public: S(){max=3;st=0;} void push(T x) { if(st==max) cout<<"\n STACK ID FULL..\n"; else { stack[st]=x; st++; cout<<"\n"<<x<<" is pushed..\n"; }

17 Stack implementation using Template void pop() { if(st==0) cout<<"\n EMPTY STACK...\n"; else { st--; cout<<"\n"<<stack[st]<<" is poped..\n"; } void show() {if(st==0) cout<<"\n EMPTY STACK..\n"; else {for(int i=0;i<st;i++) { cout<<stack[i]<<" "; } };

18 Stack implementation using Template void main() {S obj; int c,data; do {cout<<"\nSTACK\n1) PUSH\n2) POP\n3) DISPLAY"; cout >c; switch(c) {case 1: cout >data; obj.push(data);break; case 2: obj.pop(); break; case 3: obj.show(); break; case 4:break; default: cout<<"invalid choice"; break; } while(c!=4); }