EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.

Slides:



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

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++ 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.
 2006 Pearson Education, Inc. All rights reserved Templates.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
Intro to Generic Programming Templates and Vectors.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
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.
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.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
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.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Overview of C++ Templates
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
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.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
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.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
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.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
MAITRAYEE MUKERJI Object Oriented Programming in C++
TK1924 Program Design & Problem Solving Session 2011/2012
C++ Templates.
Template Classes and Functions
Chapter 14 Templates C++ How to Program, 8/e
CS212: Object Oriented Analysis and Design
Templates.
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 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.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes

Function Overloading Facilitates generalization and reuse of the code. Permits functions with the same name to co- exist as long as they have a difference in the arguments or arguments types. Compiler knows which one is the correct one to call based on the arguments supplied by the caller.

Function Overloading The operations have to be similar, but to different types of data. Nevertheless, various functions have to be written. It would be better if only one function were to be written that had a variable data type. These are called Template Functions

Template Functions Can be used to write only one function that performs identical operations to different types of data –array of int ’s, float ’s, char’ s, etc. Function is defined with a “placeholder” for the type of data to be operated on. The type is specified when the function is called.

Template Functions The syntax for template functions uses the keyword template, followed by a list of the placeholders which represent the types to be specified later, and their label. These placeholder and label pairs are contained within angle brackets ( ), each preceded by the keyword class. Then follows the function definition.

Template Functions template void function_name(int n, float a, element1_type b) { function body..... }

Template Functions- Example #include template void printArray(T *array,const int count) { for (int i=0; i<count; i++) cout << array[i] << “ “; cout << endl; }

Template Functions- Example main() { const int aCount=5, bCount=4,cCount=6; int a[aCount] = {1,2,3,4,5}; float b[bCount] = {1.1,2.2,3.3,4.4}; char c[cCount]= “HELLO”; //6th pos=null printArray(a,aCount); //int template printArray(b,bCount); //float template printArray(c,cCount); //char template return 0; }

Template Functions- Example Note that the type was specified implicitly when an int data type was passed, as in: printArray(a, aCount); Likewise, the float type was specified implicitly when the array b was passed: printArray(b, bCount); Same for the character string.

Template Functions More than one data type can be specified through the “placeholder”. Other data types can be explicitly defined directly in the function (e.g., aCount, bCount, and cCount )

Template Classes Permit the generalization of the classes so that similar classes containing different data types for the same members, do not have to be defined more than once. An example is declaring a class that contains an array whose type may differ, depending on what the user wants to put in it.

Template Classes Also called parameterized types because they require a parameter to specify how to customize the generic class. For example, an array of int’s, an array of float’s, etc. The class definition is preceded by: template

Template Classes - Example template class Stack { public: void push(const T &);. private: int size; int top; T entry[100]; };

Template Classes - Example template void Stack ::push(const T &item) { entry[++top] = item; } main() { Stack floatstack; floatstack.push(2.3);. }

Template Classes There can be more than one parameter per template class. They can be expressed as follows: template