Templates.

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

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Procedures and Control Flow CS351 – Programming Paradigms.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
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.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Overview of C++ Templates
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Templates.
Motivation for Generic Programming in C++
Procedural and Object-Oriented Programming
Programming with ANSI C ++
Jim Fawcett CSE687 – Object Oriented Design Spring 2010
How to be generic Lecture 10
C++ Templates.
Template Classes CMPS 2143.
Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Review What is an object? What is a class?
FUNCTIONS In C++.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Review: Two Programming Paradigms
Pointers and Pointer-Based Strings
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CSC 253 Lecture 8.
ADT Implementations: Templates and Standard Containers
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
References, const and classes
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
CS2011 Introduction to Programming I Methods (II)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
CIS 199 Final Review.
Java Programming Language
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)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Eighth step for Learning C++ Programming
A Deeper Look at Classes
C++ Programming CLASS This pointer Static Class Friend Class
C++ Templates An Introduction to Generic Programming.
Templates Generic Programming.
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.
Miscellaneous Topics I
Templates Generic Programming.
Presentation transcript:

Templates

Why Templates? Imagine that you need a function that to swap whatever that is sent to it as parameters. One way is to write as many overloads as possible: Void swap(int, int) void swap(float, float) Void swap(double, double) … But, it is not easy to cover everything There is more, you make a copy and paste function which is very wrong! (why?)

Template Functions Basic idea Function template swap same code is re-used for different types Function template swap takes one type parameter, T Definition interchanges values of two passed arguments of the parameterized type template <typename T> void swap(T &lhs,T &rhs) { T temp = lhs; lhs = rhs; rhs = temp; } I need to be able to call this function…

Function Templates We should (but do not have to, in some cases) specify the type of the parameters when we call a Template Function Compiler instantiates the function template definition using type int template <typename T> void swap(T &lhs,T &rhs) { T temp = lhs; lhs = rhs; rhs = temp; } int main () { int i = 3; int j = 7; swap<int> (i,j); return 0;

Function Templates Compiler infers type is really int when swap is called Compiler instantiates the function template definition using type int template <typename T> void swap(T &lhs,T &rhs) { T temp = lhs; lhs = rhs; rhs = temp; } int main () { int i = 3; int j = 7; swap (i,j); return 0;

Why Class Templates? Now, suppose that we would like to have a class Array that holds an array of anything that is given to it with any, but similar type, items One way is to create a class with many attributes of different types class Array { public: Array(const int size); ~Array(); private: int * values_; float * values_; double * values_; … const int size_; }; But again, this is not the right way!

Template Class Basic Idea: A class that can have variable attribute types Parameterized type T must be specified in class template declaration Both as a parameter, and where it’s used in the class template <typename T> class Array { public: Array(const int size); ~Array(); private: T * values_; const int size_; };

Creating Objects of Template Classes When an instance is declared, must also explicitly specify the concrete type parameter E.g., int vs. string in function main() In previous function template example, didn’t have to say swap<int> template <typename T> class Array { public: Array(const int size); ~Array(); private: T * values_; const int size_; }; int main() { Array<int> a(10); Array<string> b(5); return 0; }

Creating Objects of Template Classes What happens in the background? compiler creates a new class with the given template argument. In the previous case we have two classes: class Array { public: Array(const int size); ~Array(); private: int * values_; const int size_; }; class Array { public: Array(const int size); ~Array(); private: string * values_; const int size_; };

Template Classes with Template Functions Suppose that our Array class needs a swap function template <typename T> class Array { public: Array(const int size); ~Array(); void swap(T &lhs,T &rhs); private: T * values_; const int size_; }; Note that T is already defined for swap via the Template Class, so we do not need to declare it again

Template Classes with Template Functions: Implementing We need to implement the functions as inline (or at least in the same file) when we use templates. (or you can use .tpp files – for your own research) Why? template <typename T> class Array { public: Array(const int size); ~Array(); void swap(T &lhs,T &rhs); private: T * values_; const int size_; }; template <typename T> void Array<T>::swap(T &lhs,T &rhs) { T temp = lhs; lhs = rhs; rhs = temp; }

Notes on Templates A template parameter can also be a non-type parameter, such as an int. is an expression whose value can be determined at compile time must be: constant expressions addresses of functions or objects with external linkage addresses of static class members Non-type template arguments are normally used to initialize a class or to specify the sizes of class members. template <int N_TP> class Array { double data[N_TP]; }; Int main(){ Array<200> x; }

Template Functions as Class Members Templates can also be member functions. class A { template <typename T> void swap(T &l, T &r); }; void A::swap(T &l, T &r){}

Partial Specialization A template can be specialized for particular template arguments. template <typename T> void swap(T &v1, T &v2) { T t = v1; v1 = v2; v2 = t; } template <> void swap<Foo>(Foo &f1, Foo &f2) { swap(f1.impl_ptr, f2.impl_ptr); } A class template can also partially specialized template <typename T1, typename T2> class Foo {…}; template <typename T1> class Foo<T1, int> {…}; class Foo<T1, double> {…};

Template and static Output?

Template and static Output?

What about this?

And here? What do you think about this? template<class T = char, class U, class V = int> class X { };

Templates and Inheritance The best practice is to call template base Attributes and members using “this” pointer Or scope operator “::” This->t; This->someInt; TestBase<X>::t; TestBase<X>::someInt;

Template Member functions

Templates and Virtual keyword What is the output?

Virtual Template member functions What about this? Template member functions cannot be Declared as virtual, so the following code Has a compile time error