CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,

Slides:



Advertisements
Similar presentations
Chapter 12 Lists and Iterators. List Its an abstract concept not a vector, array, or linked structure. Those are implementations of a List. A list is a.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
C++ Programming Languages
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 18 Templates, Part II. From Last Time: What is a Template? This is the “official” specification for a template. It says that to define a template.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
BEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
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.
CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Object Oriented Programming using C++. Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Templates and Polymorphism Generic functions and classes
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
1 CSC241: Object Oriented Programming Lecture No 27.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
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.
Data Structures Using C++ 2E
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Lecture 6 : Intro. to Generic Programming Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Computer Engineering Rabie A. Ramadan Lecture 5.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Advanced Programming Rabie A. Ramadan vpro/ Lecture 1.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Overview of C++ Templates
STAT 598W Lecture 16 Templates. Templates can be used in function definitions and in class definitions. Roughly, templates are a way to (apparently) overcome.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Functions A bit of review (things we’ve covered so far)
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Motivation for Generic Programming in C++
How to be generic Lecture 10
Introduction to C++ Systems Programming.
Object-Oriented Programming (OOP) Lecture No. 17
Abstraction: Generic Programming, pt. 2
CISC/CMPE320 - Prof. McLeod
C++ Constructor Insanity CSE 333 Summer 2018
C++ Constructor Insanity CSE 333 Winter 2019
Introduction to Classes and Objects
Presentation transcript:

CS 11 C++ track: lecture 7 Today: Templates!

Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers, doubles, strings Sparse matrix of ints, doubles, complex How do we handle this?

Templates: motivation (2) Could write multiple similar classes: IntList, FloatList, DoubleList But would be almost identical except for types Adding new method means modifying many files There has to be a better way...

Templates: motivation (3) Instead can write template classes: List, List, List SparseVector, SparseVector Re-use same code for different types

Templates: example (1) template class Array { private: int len_; T* data_; int check(int i) const { if(i = len_) { throw std::string(”out of range!”); return i; } } // continued on next slide...

Templates: example (2) public: Array(int len=0) : len_(len), data_(new T[len]) {} ~Array() { delete [] data_; } int len() const { return len_; } T& operator[](int i) const { return data_[check(i)];} Array(const Array&); Array& operator=(const Array&); };

Templates: example (3) // still in Array.hh: // Copy constructor: template inline Array ::Array(const Array & other) : len_(other.len_), data_(new T[other.len_]) { for (int i = 0; i < len_; i++) { data_[i] = other.data_[i]; } } // operator= left as exercise...

Templates: example (4) // testArray.cc: #include #include ”Array.hh” int main() { Array a(10); try { a[3] = 3; a[10] = 5; // oops! } catch (std::string s) { std::cout << s << std::endl; } return 0; }

Templates: issues All the Array code is in the header file! Template is like a big macro that gets expanded to a real type when given a type argument In theory, can define template member functions in a separate class and compile separately; in practice, few compilers support this ( g++ doesn’t)

Templates: wrapup Much, much more to templates! Whole books have been written on templates Can write templated functions Can use integer template parameters Default template parameters Multiple template parameters STL (Standard Template Library) See the advanced C++ track for more