Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
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.
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.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
 2006 Pearson Education, Inc. All rights reserved Templates.
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 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.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Overview of C++ Templates
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Module 1: Array ITEI222 - Advance Programming Language.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
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.
 2006 Pearson Education, Inc. All rights reserved Templates.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Function Overloading Can enables several function Of same name
C++ Templates.
Template Classes and Functions
Introduction to C++ Systems Programming.
Chapter 14 Templates C++ How to Program, 8/e
CSC1201: Programming Language 2
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Name: Rubaisha Rajpoot
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 6: User-Defined Functions I
CSC1201: Programming Language 2
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Functions Imran Rashid CTO at ManiWeber Technologies.
TOPIC: FUNCTION OVERLOADING
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter

Sometime overloaded functions are identical A simple example: square function int square( int x) { return x*x;} float square( float x) { return x* x;} double square(double x) { return x* x;} complex square( complex x) { return x*x;} // if complex class is defined and //overloaded operators are defined

Define function template All square functions are identical in function body Only data type are different Define function templates Programmer write a single function Parameterize the data type Based on argument types provided from calls to this function, compiler generates separate object code functions to handle each function call appropriately

Define template square function template T square( T x) { return x*x;} template is the keyword followed by angle bracket with class T Class is keyword but T is a formal type name which can be named by programmer

Use template in main function #include using namespace std; template T square(T x) { return x*x;} int main() { int x = 10; float y = 3.3; double z = 4.5; cout<<“square of x”<<square(x)<<endl; cout<<“square of y”<<square(y)<<endl; cout<<“square of z”<<square(z)<<endl; return 0; }

Different definition template The formal type parameters of a template definition are used to Specify the types of the arguments Specify the return type of the function Declare variables within the function The function definition follows and is defined like any other function Function-template type can be any built-in type or user defined type

Another example: PrintArray Write a function template to print array of any type #include using namespace std; template void printArray( const T *array, const int count) { for ( int I = 0; I < count; I++) cout<< array[I]<<“ “; cout<<endl; }

printArray continue int main() { const int acount = 5; const int bcount = 7; const int ccount = 6; int a[acount] = { 1,2,3,4,5}; double b[bcount] = { 1.1,2.2,3.3,4.4,5.5}; char c[ccount] = {“HELLO”}; printArray(a,acount); printArray(b,bcount); Printarray(c,ccount); Return 0; }

A few more notes Every formal type parameter in a function- template definition must appear in the function’s parameter list at least once Formal type parameter names among function templates need not be unique In printArray, the template mechanism saves the programmer from writing 3 overloaded functions with prototypes void printarray(const int *, const int); void printarray( const double *, const int); void printArray( const char *, const int);

Overloading function template A function template may be overloaded in several ways We can provide other function templates that specify the same function name but different function parameters A function template also can be overloaded by providing non-template functions with the same name but different function arguments

Overload printArray template Overload printarray function template with additional parameters lowsubscript and highSubscript to specify the portion of the array to output template void printarray( const T *array,const int count, const int low, const int high) { // assignment to complete }