Introducing Templates and Generics in C++

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.
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.
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
T EMPLATES Chapter 11 (11.1 and 11.2 only) Department of CSE, BUET 1.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
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.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
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.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
TEMPLATE. Introduction  Template can be assumed as one that can be used to develop a function or class.  C++ use template to achieve polymorphism that.
 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.
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(
Motivation for Generic Programming in C++
TK1924 Program Design & Problem Solving Session 2011/2012
C++ Lesson 1.
Operator Overloading Ritika Sharma.
Functions + Overloading + Scope
Programming with ANSI C ++
C++ Templates.
Templates C++ template
Template Classes CMPS 2143.
Templates.
OBJECT ORIENTED PROGRAMMING
Introduction to C++ Systems Programming.
FUNCTIONS In C++.
Chapter 14 Templates C++ How to Program, 8/e
CS212: Object Oriented Analysis and Design
Templates.
Object-Oriented Programming (OOP) Lecture No. 32
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
ADT Implementations: Templates and Standard Containers
Chapter 5 Function Basics
Name: Rubaisha Rajpoot
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Anatomy of a Function Part 1
More ‘concepts’ on Function
Miscellaneous C++ Topics
Object-Oriented Programming (OOP) Lecture No. 37
Exception Handling.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
C++ Programming: chapter 8 – Templates
Really reusable software
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS1201: Programming Language 2
Anatomy of a Function Part 1
Templates C++ template
ENERGY 211 / CME 211 Lecture 23 November 12, 2008.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
More ‘concepts’ on Function
Presentation transcript:

Introducing Templates and Generics in C++ CSC 331 20081027

Overview Value of templates Generic functions Generic classes

Value of templates Added to original C++ specification Valuable in the creation of reusable code Used to create generic functions and classes In which the type of data acted upon is specified as a parameter Allows one function or class to act on several different types of data without having to code specific versions for each type.

GENERIC FUNCTIONS (a.k.a. template functions)

Generic function Defines a set of operations that are applied to various types of data. Requires the keyword “template” A pattern for what could be a function.

General form of header: Generic function General form of header: template <class Ttype> ret-type func-name (parameter list) Body: { // body of function } Example of header: template class X void swapargs(X &a, X &b)

Example generic function swapargs() template <class X> void swapargs (X &a, X &b) { X temp; temp = a; a=b; b=temp; }

Example main using swapargs() int main() { int I =10, j=20; double x=10.1, y=23.3; char a=‘x’, b=‘z’; cout<<“Original i, j “<<i<<‘ ‘<<j<<‘\n’; cout<<“Original x,y “<<x<<‘ ‘<<y<<‘\n’; cout<<“Original a,b “<<a<<‘ ‘<<b<<‘\n’; swapargs(i, j); swapargs(x, y); swapargs(a, b); cout<<“Swapped i, j “<<i<<‘ ‘<<j<<‘\n’; cout<<“Swapped x,y “<<x<<‘ ‘<<y<<‘\n’; cout<<“Swapped a,b “<<a<<‘ ‘<<b<<‘\n’; return 0; }

What happens? “template” preceding the function definition tells the compiler to create a generic (or template) function. “X” is a placeholder for the generic type. In main(), swapargs() is called using 3 different types of data. This causes the compiler to generate 3 different versions of swapargs(), each for a different type. Called “generic functions” or “specialized functions.” Act of generating referred to as “instantiating.”

More about generic functions More than one placeholder type may be declared for a function. The keyword class to specify a generic type is traditional, but keyword typename may also be used. Generic functions may be overridden for “explicit specialization.” Example: void swapargs(int &a, int &b) This “hides” the generic swapargs() only for ints. Alternative syntax: template<> void swapargs<int> (int &a, int &b) Generic functions may be overloaded (different parameter lists).

GENERIC CLASSES

Generic class Defines all the algorithms used by that class Actual type of data being manipulated is specified as a parameter. Useful when the class uses logic that can be generalized.

Example (2 generic data types) Generic class heading General form template <class Ttype> class class-name Example template <class Qtype> class queue Example (2 generic data types) template <class Type1, class Type2> class myClass

Generic class: object creation To create a specific instance of a generic class: class-name <type> ob; For previous examples: queue<int> a, b; // creates 2 integer queues myClass<int, double> ob1(10, 0.23);

Example generic class #include <iostream> using namespace std; template <class Type1, class Type2> class myClass { Type1 i; Type2 j; public: myClass(Type1 a, Type2 b){i=a; j=b;} void show() {cout<<i<<' '<<j<<'\n';} }; int main() myClass<int, double>ob1(10, 0.23); myClass<char, char *>ob2('X', "This is a test"); ob1.show(); // show int, double ob2.show(); // show char, char * system("pause"); return 0; }