Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.

Slides:



Advertisements
Similar presentations
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.
Advertisements

14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
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.
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 & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
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.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
1 CSC241: Object Oriented Programming Lecture No 25.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Motivation for Generic Programming in C++
C++ Lesson 1.
Programming with ANSI C ++
C++ Templates.
Generic Algorithms (TIC++V2:C6)
Templates.
Template Classes and Functions
Introduction to C++ Systems Programming.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Command Line Arguments
Chapter 14 Templates C++ How to Program, 8/e
Chapter 5 Function Basics
Functions and an Introduction to Recursion
Programming fundamentals 2 Chapter 1:Array
Chapter 5 Function Basics
Pointers and Pointer-Based Strings
Templates.
Object-Oriented Programming (OOP) Lecture No. 32
Function Basics.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
CSC1201: Programming Language 2
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Chapter 5 Function Basics
Name: Rubaisha Rajpoot
More About Data Types & Functions
Screen output // Definition and use of variables
Data type List Definition:
Pointers & Functions.
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
Pass by Reference.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Engineering Problem Solving with C++, Etter
Functions and an Introduction to Recursion
Parasol Lab, Texas A&M University
CHAPTER 2 Arrays and Vectors.
Pointers and Pointer-Based Strings
Templates I CMSC 202.
CHAPTER 2 Arrays and Vectors.
CMSC 341 C++ and OOP.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers and dynamic objects
Pointers & Functions.
TOPIC: FUNCTION OVERLOADING
CMSC 341 C++ and OOP.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar in passing value/variable into function. Instead of passing a variable, we pass a type (such as int, double, and Point) into template. Passing type is known as generic programming, as we can program in generic type and invoke the code using a specific type.

The goal of generic programming is to write code that is independent of the data types. In C language, all codes are tied to a specific data type. For container data structures (such as array and structure), you need to specify the type of the elements. For algorithms (such as searching or sorting), the code works only for a specific type, you need to rewrite the code for another type. Can we write a single sorting routine that works on all types (or most of the types) by specifying the type during invocation? Can we have a general container that can work on all types?

template lets you program on generic type, instead of on a specific type. Template supports so-called parameterized type - i.e., you can use type as argument in building a class or a function (in class template or function template). Template is extremely useful if a particular algorithm is to be applied to a variety of types, e.g., a container class which contains elements, possibly of various types.

A template can be used to create a family of classes and functions. For example , a class template for an array class would enable us to create array of various type such as int array , float array and double array . Similarly ,we can define a template for a function like “mul()” that can help us to create various version of “mul()” for mltiplying “int” , “float” or “double” type value. Templates are also called “parameterized classes or function”

template <typename T> void mySwap(T &a, T &b) { T temp; temp = a; a = b; b = temp; } #include <iostream> using namespace std; template <typename T> void mySwap(T &a, T &b); int main() { int i1 = 1, i2 = 2; mySwap(i1, i2); // Compiler generates mySwap(int &, int &) cout << "i1 is " << i1 << ", i2 is " << i2 << endl; char c1 = 'a', c2 = 'b'; mySwap(c1, c2); // Compiler generates mySwap(char &, char &) cout << "c1 is " << c1 << ", c2 is " << c2 << endl; double d1 = 1.1, d2 = 2.2; mySwap(d1, d2); // Compiler generates mySwap(double &, double &) cout << "d1 is " << d1 << ", d2 is " << d2 << endl; }

void mySwap(int &a, int &b) take note that the C++ compiler generate a version of the code for each type used in the program, in a process known as instantiation of template. For example, with type of int, the following code will be generated by the compiler: void mySwap(int &a, int &b) { int temp; temp = a; a = b; b = temp; }

/* Test function Template */ #include <iostream> using namespace std; template<typename T> T abs(T value) { T result; // result's type is also T result = (value >= 0) ? value : value; return result; } int main() { int i = 5; cout << abs(i) << endl; double d = 55.5; cout << abs(d) << endl; float f = 555.5; cout << abs(f) << endl; Note:The compiler creates three versions of the  abs function based on the function template, for types of int, double, and float.

#include <iostream> using namespace std; /* C++ program to display larger number among two numbers using function templates. */ /* If two characters are passed to function template, character with larger ASCII value is displayed. */ #include <iostream> using namespace std; template <typename T> T Large(T n1, T n2) { return (n1>n2) ? n1:n2; } Void main() { int i1, i2; float f1, f2; char c1, c2; cout<<"Enter two integers: "; cin>>i1>>i2; cout<<Large(i1, i2)<<" is larger."; cout<<"\n\nEnter two floating-point numbers: "; cin>>f1>>f2; cout<<Large(f1, f2)<<" is larger."; cout<<"\n\nEnter two characters: "; cin>>c1>>c2; cout<<Large(c1, c2)<<" has larger ASCII value.";

The following is the example of a function template that returns the maximum of two values: #include <iostream> #include <string> using namespace std; template <typename T> inline T const& Max (T const& a, T const& b) { return a < b ? b:a; } int main () int i = 39; int j = 20; cout << "Max(i, j): " << Max(i, j) << endl; double f1 = 13.5; double f2 = 20.7; cout << "Max(f1, f2): " << Max(f1, f2) << endl; string s1 = "Hello"; string s2 = "World"; cout << "Max(s1, s2): " << Max(s1, s2) << endl; return 0; }

Class Templete Templates allows us to define generic class. It is a simple perocess to create a geeric class using with an anonymous class. The general format of the class t emplate<class T> class classnam { //class member specification // with anonyoums type T // whenever appropriate };

# include<iostream.h> Using namespace std Template <class T1 , class T2> class test { T1 a ; T2 b; Public : test ( T1 x, T2 y) { a=x; b=y; } Void show() cout << a << “and “<< b << “\n”; }; void main() { Test <float,int> test1 (1.23, 123); T est< int , char> test2(100,’W’); test1.show(); test2.show(); } Output: 1.23 and 123 100 and W