Intro to Generic Programming Templates and Vectors.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.
Generics and the ArrayList Class
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
OOP Etgar 2008 – Recitation 51 Object Oriented Programming Etgar 2008 Recitation 5.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
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.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
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.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
1 CSC241: Object Oriented Programming Lecture No 25.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
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.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
 2006 Pearson Education, Inc. All rights reserved Templates.
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++
Programming with ANSI C ++
C++ Templates.
Templates.
Exceptions, Templates, and the Standard Template Library (STL)
Templates.
Java Programming Language
Name: Rubaisha Rajpoot
CMSC 202 Templates.
CMSC 202 Lesson 22 Templates I.
Exceptions and Templates
Abstraction: Generic Programming, pt. 2
Exceptions, Templates, and the Standard Template Library (STL)
C++ Templates CSE 333 Summer 2018
Templates I CMSC 202.
Java Programming Language
C++ Templates An Introduction to Generic Programming.
Templates An introduction.
Templates CMSC 202, Version 4/02.
Corresponds with Chapter 5
Presentation transcript:

Intro to Generic Programming Templates and Vectors

Why? How? Writing the same search, sort, or other functionality for all the different data types is tedious and error prone. Why not use a solution that allows you to write a function once and then use it for any kind of data? In C++ generic programming is implemented with “templates”

Simple Template Functions template T findMax(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x = 0; cout << findMax(x, ++x); // prints 1 cout << x; // prints 1 string s = “hello”; string t = “world”; cout << findMax(s, t); // prints “world” }

What’s all this? Templates have ugly syntax. Get used to it, and on an exam, be able to “get in the ballpark” of the correct syntax. Conceptually, templates are a lot like macros, just without the unpleasant side effects. – When you see a template, or use a template, think “text substitution” and you’ll be close

Template Instantiation A template definition is a recipe that the compiler can use to generate a function (or a class, more on that later) The compiler will not use this recipe unless/until you instantiate the template. – At that point, the compiler goes and performs the text substitution you asked for, and then compiles the newly generated function as if you’d written that function yourself. What happens if I instantiate the same template multiple different ways? – Well, with function overloading, we just get two or more functions with the same name, but with different arguments!

Example (template definition) template T findMax(T x, T y) { if (x < y) { return y; } else { return x; } } “T” is the template parameter. Since the “T” is specified as a “typename” (i.e., the name of some type), then “T” can be replaced by any type (e.g., “int” or “string”). T can NOT be replaced by any arbitrary text, just by a type. We have “defined” this template, which means the compiler now knows the recipe. But there is no machine code for the max function yet. The compiler won’t actually compile the max function until we instantiate it. – The compiler does do some preliminary syntax checking, so you can get compiler errors in your template definitions even if you don’t instantiate them.

Template Classes C++ also provides template classes. Virtually any “data structure” (AKA “collection”, AKA “container”) will be implemented in C++ as a template – The type of data stored in the structure is really not at all relevant to the data structure itself. Template classes get “defined” and “instantiated” in analogous ways to template functions with the following caveats – The compiler will never guess at the template argument for a template class, you must always explicitly tell the compiler “what T is”. – Classes cannot be “overloaded”, but the compiler will permit you to instantiate the same template class in multiple ways. Each distinct instantiation results in a completely distinct class! (with its own copy of the static data members, for example). – The member functions in a template class are template functions (oh, how confusing!)

A Useful Example (abridged) template class vector { public: void push_back(T); void pop_back(void); T& operator[](int k); explicit vector(int initial_capacity=8); private: T* data; int length; int capacity; void expand(void); // increase capacity };