Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.

Slides:



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

Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
Functions Prototypes, parameter passing, return values, activation frams.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Passing Streams to Functions. Passing Streams to Functions One Rule: always pass a stream as a reference.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
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.
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.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Function Overloading. 2 Function Overloading (Function Polymorphism) Function overloading is a feature of C++ that allows to create multiple functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
CS 240: Data Structures Supplemental: Command Line Input.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 7 Functions.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Instructor - C. BoyleFall Semester
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Data Types Storage Size Domain of all possible values Operations 1.
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.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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(
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Operator Overloading Part 2
Generic programming – Function template
Templates.
Multiple Files Revisited
Working With Arrays.
Introduction to Programming
CMSC 202 Lesson 22 Templates I.
The Run-Time Stack and Reference Parameters
Default Arguments.
C++ Compilation Model C++ is a compiled language
CS150 Introduction to Computer Science 1
Function Overloading.
CS150 Introduction to Computer Science 1
Templates I CMSC 202.
CMSC 202 Lesson 6 Functions II.
Class: Special Topics Overloading (methods) Copy Constructors
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Templated Functions

Overloading vs Templating  Overloaded functions allow multiple functions with the same name but different functionality.  Templated functions allow for different types of parameters but have exactly the same functionality.

Template Specifics  Template functions are NOT prototyped.  Templated code must be placed…  above the main function if your program is in one file  in a header file if your program is in multiple files

Syntax template void swap ( T & t1, T & t2) { T temp = t1; t1 = t2; t2 = temp; return; }

Syntax template void swap ( T & t1, T & t2) { T temp = t1; t1 = t2; t2 = temp; return; }

Syntax template void swap ( T & t1, T & t2) { T temp = t1; t1 = t2; t2 = temp; return; }

Example template void swap (T & t1, T & t2) { T temp = t1; t1 = t2; t2 = temp; return; } int main() { int a = 8, b = 3; float c = 4.3, d = 98.5; char c1 ='y', c2 = swap ( a, b ); swap ( c1, c2 ); swap ( c, d ); return 0; }

Example template void swap (int& t1, int& t2) { int temp = t1; t1 = t2; t2 = temp; return; } int main() { int a = 8, b = 3; float c = 4.3, d = 98.5; char c1 ='y', c2 = swap ( a, b ); swap ( c1, c2 ); swap ( c, d ); return 0; }

Example template void swap (char& t1, char& t2) { char temp = t1; t1 = t2; t2 = temp; return; } int main() { int a = 8, b = 3; float c = 4.3, d = 98.5; char c1 ='y', c2 = swap ( a, b ); swap ( c1, c2 ); swap ( c, d ); return 0; }

Example template void swap (float& t1, float& t2) { float temp = t1; t1 = t2; t2 = temp; return; } int main() { int a = 8, b = 3; float c = 4.3, d = 98.5; char c1 ='y', c2 = swap ( a, b ); swap ( c1, c2 ); swap ( c, d ); return 0; }

The Importance of Documentation // Pre: The type to fill the template parameter must have // the insertion operator defined for it. template void print (const U & u) { cout << u; return; } // Pre: The type of the template parameter must have the // T_type min_value (const T_type & t1, const T_type & t2) { return (t1 < t2 ? t1 : t2); }

Templating Two Types // Pre: Both template types must have insertion // operator defined. template void repeater (const int num_times, const T t1, const U u1) { for (short i = num_times; i > 0; i--) cout << t1 << “ ” << u1 << endl; return; }

// Pre: Both template types must have insertion // operator defined. template void repeater (const int num_times, const T t1, const U u1) { for (short i = num_times; i > 0; i--) cout << t1 << “ ” << u1 << endl; return; } int main() { char some_character = ‘$’; float a_float_value = 2.2; repeater(4, some_character, a_float_value); return 0; }

// Pre: Both template types must have insertion // operator defined. template void repeater (const int num_times, const T t1, const U u1) { for (short i = num_times; i > 0; i--) cout << t1 << “ ” << u1 << endl; return; } int main() { char some_character = ‘$’; float a_float_value = 2.2; repeater(4, some_character, a_float_value); return 0; } $ 2.2 $ 2.2 output

End of Session