AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: 0511261 Fall 2014 Templates.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
Multiple-Subscripted Array
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
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.
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
 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 Using Templates COSC 1567 C++ Programming Lecture 10.
1 CSC241: Object Oriented Programming Lecture No 25.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Overview of C++ Templates
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
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.
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:\Temp\Templates 4 5 Use This Main Program 6.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
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.
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++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
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(
C++ Templates.
Template Classes and Functions
Name: Rubaisha Rajpoot
Introduction to Programming
Introduction to Programming
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CS150 Introduction to Computer Science 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Templates

Function Templates To write invers function that Receive different type we write: Function templates: allow you to create generic functions that have the same bodies but can take different data types as parameters It’s a powerful feature supported by C++ language

Using function templates Rewrite invers function using template we can : In a function template, at least one parameter is generic, or parameterized, meaning that one parameter can stand for any number of C++ types. T is simply a placeholder for the type that will be used at each location in the function definition where T appears. keyword class in the template definition does not necessarily mean that T stands for a programmer-created class type, but it may. The compiler generates code for as many different functions as it needs, depending on the function calls that are made

Example The following Swap function can be used to interchange between two integer numbers void swap (int &x, int &y){ int temp=x; x=y; y=temp;} The following Swap function can be used to interchange between two character items void swap (char &x, char &y){ char temp=x; x=y; y=temp;} To avoid repetition of function we can use template function: template void swap (R &x, R &y){ R temp=x; x=y; y=temp;}

Using more than one parameterized type in a function template To create a function template that employs multiple generic types, you simply use a unique type identifier for each type. For example, suppose you want to create a display () function that displays two values template void display (T val1, U val2) { cout << "First is " << val1 << " Second is " << val2 << " "; } val1 and val2 can be different types.

Using class template Class template allow you to generate a class in which at least one type is generic or parameterized. To create a class template, you begin with the template definition, just as you do with a function template. Then you write the class definition using the generic type Example: template class Number { private: T Number; public: Number(T n): Number(n){}; void displayNumber(){cout<< Number;} }; To defined object myValue from template class we use: Number myValue(25); // ClassName ObjectName

Example #include template class Number { private: T theNumber; public: Number(T n); void display (); }; template Number ::Number(T n) { theNumber = n;} template Void Number ::display () { cout << "Number # "; cout << theNumber << endl; } int main() { Number anInt(65); Number aDouble(3.46); Number aChar('K'); Number aCharAsAnInt('K'); Number anIntAsAChar(65); anInt.display (); aDouble.display (); aChar.display (); aCharAsAnInt.display (); anIntAsAChar.display (); return 0; }

Creating an Array template Class #include template class Array{ private: T *data; // T is the type of the array int size; public: Array(T *d, int s); void showList(); void showFirst(); }; template Array ::Array(T *d, int s){ data = d; size = s; } template void Array ::showList(){ cout << "Entire list:" << endl; for(int x = 0; x < size; ++x) cout << data[x] << endl; cout << " " << endl;} template void Array ::showFirst(){ cout << "First element is "; cout << data[0] << endl;} int main(){ int nums[4]={1,2,3,4}; Array MyIntArray(nums, 4); MyIntArray.showList(); MyIntArray.showFirst(); char ch[5]={'A','B','C','D'}; Array MyCharArray(ch, 5); MyCharArray.showList(); return 0;}

Defined classes In c++ we can save classes implementation in.h file. And in man function we can import it #include #include “XClass.h" class Yclass { int i; public: X x; Yclass() { i = 0; } void f(int b) { i = b; } int read() { return i; } }; int main() { Yclass y; y.f(47); y.x.set(37); cout<<y.x.read(); cout<<y.read(); } #ifndef XCLASS_H #define XCLASS_H class X { int i; public: X() { i = 0; } void set(int ii) { i = ii; } int read() const { return i; } int permute() { return i = i * 47; } }; #endif Save it as XClass.h in same working folder

Exercise Class in UML Write C++ program To implement classes