Templates Generic Programming.

Slides:



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

1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
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.
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
 2006 Pearson Education, Inc. All rights reserved Templates.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
ECE122 Feb. 22, Any question on Vehicle sample code?
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
CS212: Object Oriented Analysis and Design
Overview of C++ Templates
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Lec 14 Writing an Instantiable Class III. Agenda Review of Instantiable Classes Scope of variables Using this to override scope issues Lab: Creating a.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Introduction to Object Oriented Programming Chapter 10.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
 2006 Pearson Education, Inc. All rights reserved Templates.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
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(
TK1924 Program Design & Problem Solving Session 2011/2012
Procedural and Object-Oriented Programming
CS 2304: Templates And Compilation
Programming with ANSI C ++
Chapter 13: Overloading and Templates
How to be generic Lecture 10
Templates.
Exceptions, Templates, and the Standard Template Library (STL)
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 14 Templates C++ How to Program, 8/e
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CS212: Object Oriented Analysis and Design
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ for Engineers and Scientists Second Edition
Operator Overloading
Defining Classes and Methods
Name: Rubaisha Rajpoot
CMSC 202 Templates.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Overview of C++ Overloading
Comments, Prototypes, Headers & Multiple Source Files
Sridhar Narayan Java Basics Sridhar Narayan
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 9: Value-Returning Functions
Java Programming Language
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Data Structures and ADTs
Templates An introduction.
Function Templates Class Templates
Class rational part2.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
Constructors & Destructors
Corresponds with Chapter 5
Templates Generic Programming.
Methods Scope How are names handled?
Object Oriented Programming
Presentation transcript:

Templates Generic Programming

The Issue Want function to find the larger of two values

Up Until Now Need multiple versions: int largest(int num1, int num2) { //return largest } double largest(double num1, double num2) { //return largest } Person largest(Person p1, Person p2) { //return largest }

Templates Templated Functions Specify way to build a function Applies to any data type

Template Declaration template<class/typename T> class/typename interchangeable T is a type parameter – can be anything Can have multiple type parameters

Type Parameter Type parameter used to code template: Whatever T is… We return one We take two as parameters

Instantiation Template instantiated when needed at compile time New version of code created for each type template applied to

Template Including Template functions must have full code in .h file Needed at compile time for instantiation Don’t have to worry about multiple versions

Templates All T's must be same type No conversions done

Templates All T's must be same type No conversions done Can force type

Operators & Members Template can assume any operators/members it wants Compile error if type doesn't support

Non Templated Params Can have normal parameters in template function

Templated Variables Can use template type as local variable type

Templated Class Class can be template Type of variable includes instantiation type:

Templated Class Functions Function definitions must be template Scope resolution must include template

Templates of Templates Template can instantiate on templated type:

Multiple Types Template can have multiple types: