Templates Generic Programming.

Slides:



Advertisements
Similar presentations
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Advertisements

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.
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.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C/c++ 4 Yeting Ge.
 2006 Pearson Education, Inc. All rights reserved 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.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Classes. Class expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
1 Joe Meehean.  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
#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.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
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(
Operator Overloading.
TK1924 Program Design & Problem Solving Session 2011/2012
Andrew(amwallis) Classes!
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
C++ Templates.
Template Classes CMPS 2143.
Templates.
Encapsulation, Data Hiding and Static Data Members
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 14 Templates C++ How to Program, 8/e
CS212: Object Oriented Analysis and Design
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
Name: Rubaisha Rajpoot
CMSC 202 Templates.
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CSC 253 Lecture 7.
Reference Parameters.
Manipulating Pictures, Arrays, and Loops part 3
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Tonga Institute of Higher Education
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Pointers Pointers point to memory locations
Templates I CMSC 202.
Yan Shi CS/SE 2630 Lecture Notes
Really reusable software
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
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.
COP 3330 Object-oriented Programming in C++
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
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(const Person& p1, const 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 https://www.tutorialspoint.com/compile_cpp_online.php

Exe 1 Go to https://www.tutorialspoint.com/compile_cpp _online.php Implement the Min method following the idea of Slide #4 Show me the output

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

Exe 2 Try to call Max of Exe 1 with f1 and j What is the key step to make it to work?

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: