Lecture 17: 4/4/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
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.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
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.
1 Chapter 17-1 Templates and Exceptions Dale/Weems.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
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.
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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Copyright 2006, The Ohio State University Introduction to C++ Templates l C++ Function Templates l C++ Class Templates.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
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)
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Chapter 17 Templates and Exceptions Dale/Weems.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
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.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
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.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Templates.
Chapter 14 Templates C++ How to Program, 8/e
Generic programming – Function template
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS148 Introduction to Programming II
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Presentation transcript:

Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 17: 4/4/2003

CS148 Spring Outline Function overloading Function templates Chapter 17

Lecture 17: 4/4/2003CS148 Spring Generic Algorithms Sometimes, we need a single algorithm that might be applied to objects of different data types at different times We need to be able to describe the algorithm without having to specify the data types of the items being manipulated Such algorithm is referred to as a Generic algorithm C++ supports generic algorithms using two mechanisms  Function overloading  Function templates

Lecture 17: 4/4/2003CS148 Spring Function overloading //print an integer variable void print (string vname, int n) { cout << "Value of " << vname << " = " << n <<endl; } //print a character variable void print (string vname, char ch) { cout << "Value of " << vname << " = " << ch <<endl; } //print a float variable void print (string vname, float fl) { cout << "Value of " << vname << " = " << fl <<endl; } Function overloading is the use of the same name for different functions, as long as their parameters types are sufficiently different for the compiler to tell them apart There are really 3 distinct print functions The compiler generates distinct names for each function When it encounters a call to print, it must determine which function to invoke. The compiler compares the type of the second argument with the type of the second parameter. If the second argument is of type int, the compiler generates code to the call the print function that has a second int parameter

Lecture 17: 4/4/2003CS148 Spring Function templates 1/2 using function overloading has its benefits, but we had to supply three different function definitions (nearly identical functions) We use a function template, which allows us to write a function definition and leave out the data types //print a variable name and its value using a function template //SomeType is called the template parameter template void print (string vname, SomeType val) { cout << "Value of " << vname << " = " << val <<endl; } Syntax for function template Template FunctionDefinition TemplateParamList is a sequence of one or more parameter declarations separated by commas, each defined as classidentifer or typename identifier

Lecture 17: 4/4/2003CS148 Spring Function templates 2/2 //print a variable name and its value //SomeType is called the template parameter template void print (string vname, SomeType val) { cout << "Value of " << vname << " = " << val <<endl; } int main (){ int n = 10; char ch = 'a'; float fl = 3.5; print ("n",n); print ("ch",ch); print ("fl",fl); return 0; }// end main () At compile time, the compiler generates three different functions and gives its own internal name to each of these functions (because there are 3 function calls in the main function) The three new functions are called template functions or generated functions as opposed to the function template from which they were created Another form of making a function call is print (“n”,n); where int enclosed in angle brackets is called the template argument If the template argument is omitted, it is deduced from the data type of the second argument