Yan Shi CS/SE 2630 Lecture Notes

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Chapter 17-1 Templates and Exceptions Dale/Weems.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
Yan Shi CS/SE2630 Lecture Notes
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 22 - C++ Templates
Programming with ANSI C ++
Chapter 6 CS 3370 – C++ Functions.
How to be generic Lecture 10
C++ Templates.
Templates.
7. Inheritance and Polymorphism
FUNCTIONS In C++.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
A First C++ Class – a Circle
CS212: Object Oriented Analysis and Design
Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Built-In (a.k.a. Native) Types in C++
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
Classes and Objects.
Miscellaneous C++ Topics
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Submitted By : Veenu Saini Lecturer (IT)
Really reusable software
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Templates An introduction.
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Function Templates Class Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lecture 8: Introduction to C++ Templates and Exceptions
Templates Generic Programming.
Presentation transcript:

Yan Shi CS/SE 2630 Lecture Notes 5. Template Yan Shi CS/SE 2630 Lecture Notes

Void Pointer void * p; // Declare like a normal pointer, use the // void keyword as the pointer type. AKA generic pointer: can be pointed at objects of any data type! int num; float value = 1.2; p = &num; p = &value; One of its possible uses may be to pass generic parameters to a function. cannot be dereferenced! We don’t know the type.. cast the void pointer before dereference it cout << *static_cast<float*>( p ); also cannot do pointer arithmetic! same reason. Avoid using void pointers if possible. There are other ways to achieve genetic functions! If a void pointer doesn’t know what it’s pointing to, how do we know what to cast it to? Ultimately, that is up to you to keep track of. In general, it is a good idea to avoid using void pointers unless absolutely necessary, as they effectively allow you to avoid type checking. This allows you to inadvertently do things that make no sense, and the compiler won’t complain about it.

Generic What is the purpose of typedef Fruit InfoType; To make your operation/data structure more generic, we can use a template function/class. Template: a C++ language construct that allows the compiler to generate multiple versions of a class type or a function by allowing parameterized type.

Function Template To declare: template<class Type> Type max(const Type &a, const Type &b) { return a > b ? a : b; } … max<int>( 5, 9 ); max<Student>( mike, alex ); To declare: template <class identifier> function_declaration; template <typename identifier> function_declaration; To use: function_name <type> (parameters); Make sure the operations defined in the function template are defined for the type of objects that are using the template.

Function Template In some cases the compiler can automatically find out the type. We can also define function templates that accept more than one type of parameter: int i,j; max(i,j); template <class T, class U> T GetMin (T a, U b) { return ( a < b ? a : b ); } … int i,j; long l; i = GetMin<int,long> (j,l);

Class Template template<class ItemType> class Stack { protected: int height; ItemType items[50]; public: Stack() : height(0) { } void push(ItemType x) { items[height] = x; ++height; } ItemType pop() { height--; return items[height]; } bool isEmpty() const { return height <= 0; } bool isFull() const { return height >= 50; } }; Stack<Student> stuStack; Stack<int> nums; nums.push(10);

Template Class At compile time, the compiler generates distinct class types and gives its own internal name to each type. Stack_Student stuStack; Stack_int nums; The new class types are called template classes. Similar to template functions.

Implementing Class Template Two options: put all code for template in .h file //preferred! in the template implementation file (.tpp), precede the member function definition with the template <…> prefix and include this .tpp file at the end of .h file An implementation trick: drive template class from non-template class, put most code in base class template <class ItemType> void Stack<ItemType>::push(ItemType x) { items[height] = x; ++height; } class ComplexContainer { ... }; template<class T> class Container : public ComplexContainer {...};

Non-Type Parameters Can also have non-type parameters: template<class ItemType, int size> class Stack { protected: int height; ItemType items[size]; public: Stack() : height(0) { } void push(ItemType x) { items[height] = x; ++height; } ItemType pop() { height--; return items[height]; } bool isEmpty() const { return height <= 0; } bool isFull() const { return height >= size; } } … Stack<int, 100> st;

Type Checking Type checking is guaranteed when using class templates Solution: make DoIt a function template! void DoIt(Stack<char, 10> chs) {…} Stack<char, 20> letters; letters.push(“something”); //illegal! DoIt(letters); //illegal! template<class T, int size> void DoIt(Stack<T, size> stack) {…} Stack<char, 20> letters; DoIt(letters); //OK!

inline Function keyword inline: function is treated as a macro template<class T> inline T sqr(const T &x) { return x * x; } the preprocessor converts the code cout << sqr(5); into cout << 5 * 5; Pros: no function call overhead compiler optimization Cons: Expand code if inline function is long hard to debug: it looks like a function is being called, but it's not really inline functions should only be in header files! compiler must see body of inline function to do the job. class members defined within class definition are automatically inlined.