CS212: Object Oriented Analysis and Design

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Generic programming in Java
Chapter 5 Functions.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Chapter 14: Overloading and Templates
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2006 Pearson Education, Inc. All rights reserved Templates.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 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.
 2006 Pearson Education, Inc. All rights reserved Generics.
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.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
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.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Learners Support Publications Functions in C++
Introduction to Generics
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
1 CSC241: Object Oriented Programming Lecture No 25.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Overview of C++ Templates
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Introduction to Object-Oriented Programming Lesson 2.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
CS212: Object Oriented Analysis and Design Lecture 30: Namespace and Other Advanced Topics.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
 2006 Pearson Education, Inc. All rights reserved Templates.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Generic programming – Function template
CS212: Object Oriented Analysis and Design
Classes and Objects.
Java – Inheritance.
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 21: Introduction to Templates

Recap of Lecture 20 Derived class exception Special scenarios Restricting throw Rethrow Terminate() and unexpected()

Outline of Lecture 21 Introduction to Templates Function Templates Overloading templates Specialization

Introduction Sophisticated programming feature It allows to construct both functions and classes based on types that have not yet been stated Powerful tool for automating program code generation One function or class can be used with several different types of data without having to explicitly recode specific versions for each data type. Provide a simple way to represent a wide range of general concepts and simple ways to combine them.

Templates A function template defines a group of statements for a function using a parameter instead of a concrete type (Algorithm) A class template specifies a class definition using a parameter instead of a concrete type (parameterized type)

Advantages of Templates A template need only be coded once. Individual functions or classes are automatically generated when needed. A template offers a uniform solution for similar problems Allows type-independent code to be tested early in the development phase. Errors caused by multiple encoding are avoided. Policy-Based Class Design

Function Template A function template definition (or declaration) is always preceded by a template clause template <class T> T Max (T, T); Declares a function template template <class T> T Max (T val1, T val2) { return val1 > val2 ? val1 : val2; } Defines the function

Type parameter It is an arbitrary identifier whose scope is limited to the function itself Type parameters always appear inside <> Each type parameter consists of the keyword class followed by the parameter name Multiple type parameters should be separated by commas Each specified type parameter must be referred to in the function prototype

Type parameter: Example template <class T1, class T2, class T3> T3 Relation(T1, T2); // ok template <class T1, class T2> int Compare (T1, T1); // illegal! template <class T1, T2> // illegal! int Compare (T1, T2); template <class T> inline T Max (T val1, T val2); // ok inline template <class T> // illegal! T Max (T val1, T val2);

Function Template Instantiation A function template represents an algorithm Functions are generated by the compiler by binding its type parameters to concrete (built-in or user-defined) types The compiler does not attempt any implicit type conversions to ensure a match. Demonstration

Overloading a Generic Function Function templates can be overloaded in exactly the same way as normal functions This is formally called explicit specialization Overloaded function overrides (or "hides") the generic function relative to that specific version Allows tailoring of a version of a generic function to accommodate a unique situation Use overloaded functions rather than templates

Generic Function Restrictions Similar to overloaded functions except that they are more restrictive Overloading: different actions performed within the body of each function Generic function: generic function must perform the same general action for all versions Demonstration

Compacting an Array This function compacts the elements in an array Remove unused elements from the middle of an array All unused elements are at the end compact() Demonstration

Thank you Next Lecture: Class Templates