C++ Template.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
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.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Copyright 2006 Pearson Addison-Wesley, 2008, 2009 Joey Paquet 9-1 Concordia University Department of Computer Science and Software Engineering COMP345.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
1 C++ Classes & Object Oriented Programming Overview & Terminology.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Operator Overloading.
Operator Overloading Introduction
C++ Lesson 1.
Programming with ANSI C ++
Submission Example May 14, 2018
Polymorphism Templates
C++ Templates.
Templates.
OBJECT ORIENTED PROGRAMMING
Introduction to C++ Systems Programming.
A First C++ Class – a Circle
Templates.
Polymorphism Lec
Name: Rubaisha Rajpoot
CMSC 202 Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Pointers & Functions.
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Andy Wang Data Structures, Algorithms, and Generic Programming
Overview of C++ Overloading
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 6: User-Defined Functions I
Templates I CMSC 202.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers and dynamic objects
Pointers & Functions.
C++ Templates An Introduction to Generic Programming.
Final Exam Review Inheritance Template Functions and Classes
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
Presentation transcript:

C++ Template

Introduction A general definition of functions and classes Further simplifies the previous function overloading Another form of polymorphism In a template, the data type is the parameter! The precise definitions of the template functions and classes are determined in run-time based on the data type!

Function Templates Consider the following function int max (int x, int y) { return (x > y? x : y); } It only works for the int type of variables! How can we make it work for other types of variables?

Function Templates Use function overloading! float max (float x, float y) { return (x > y? x : y); } double max (double x, double y) { return (x > y? x : y); } char max (char x, char y) { return (x > y? x : y); } ……

Function Templates Do we have a smarter way to do that? Use function overloading! float max (float x, float y) { return (x > y? x : y); } Do we have a smarter way to do that? double max (double x, double y) { return (x > y? x : y); } char max (char x, char y) { return (x > y? x : y); } ……

Function Templates! First line called "template prefix" template <class T> T max (T x, T y) { return (x > y? x : y); } Type parameter First line called "template prefix" Tells compiler what’s coming is "template" And that T is a type parameter

Function Templates! First line called "template prefix" template <class T> T max (T x, T y) { return (x > y? x : y); } First line called "template prefix" Tells compiler what’s coming is "template" And that T is a type parameter In template <class T>, "class" means "type", or "classification“! It is NOT the class we learned before!

Function Templates! First line called "template prefix" template <class T> T max (T x, T y) { return (x > y? x : y); } First line called "template prefix" Tells compiler what’s coming is "template" And that T is a type parameter In template <class T>, "class" means "type", or "classification“! It is NOT the class we learned before! To avoid confusion, one can use “typename”.

Function Templates! T can be replaced by any type in the run time template <class T> T max (T x, T y) { return (x > y? x : y); } T can be replaced by any type in the run time Predefined or user-defined (like a C++ class type) In function definition body: T used like any other type, just like it has been defined already Note: one can use other symbol than "T", but T is the "traditional" usage

How Does this Work? Consider the following example #include <iostream> using namespace std; template<typename T> T abs(T x) { return x < 0? -x : x; } int main() { int n = -5; double d = -5.5; cout << abs(n) << endl; cout << abs(d) << endl; return 0; Output is 5 5.5

How Does this Work? Consider the following example #include <iostream> using namespace std; template<typename T> T abs(T x) { return x < 0? -x : x; } int main() { int n = -5; double d = -5.5; cout << abs(n) << endl; cout << abs(d) << endl; return 0; If not using template, at least two abs functions need to be defined: One for int type, the other for double type Output is 5 5.5

How Does this Work? Consider the following example #include <iostream> using namespace std; template<typename T> T abs(T x) { return x < 0? -x : x; } int main() { int n = -5; double d = -5.5; cout << abs(n) << endl; cout << abs(d) << endl; return 0; In run-time, compiler determines the actual type for T based on the type of the input variable

How Does this Work? Consider the following example #include <iostream> using namespace std; template<typename T> T abs(T x) { return x < 0? -x : x; } int main() { int n = -5; double d = -5.5; cout << abs(n) << endl; cout << abs(d) << endl; return 0; In run-time, compiler determines the actual type for T based on the type of the input variable For instance, abs(n), n is int type

How Does this Work? Consider the following example #include <iostream> using namespace std; template<typename T> T abs(T x) { return x < 0? -x : x; } int main() { int n = -5; double d = -5.5; cout << abs(n) << endl; cout << abs(d) << endl; return 0; In run-time, compiler determines the actual type for T based on the type of the input variable For instance, abs(n), n is int type The compiler then creates the following function based on the template int abs(int x) { return x < 0? -x : x; }

Difference Between Template Functions and Normal Functions Compiler won’t generate instance for template functions during compiling. It generates when it is called (see abs example). If a template function is used by several other places in different .cpp files, the template function and its body need to be put in the .h file not only the declaration. The function pointer can only point to instance of the template function.

Back to the max function We now can use template to address our issue with the max function! #include <iostream> using namespace std; template<class T> T max(T x, T y) { return x > y? x : y; } void main() { int n1 = -5, n2 = 0; double d1 = -5.5, d2 = -5.9; cout << max(n1, n2) << endl; cout << max(d1, d2) << endl;

Swap values example #include <iostream> using namespace std; template<class T> T swapVals(T &x, T &y) { T tmp = x; x = y; y = tmp; } class ABC{ public: ABC(float inx=0, float iny=0) {x=inx; y=iny;} private: float x, y; }; void main() { int n1 = -5, n2 = 0; double d1 = -5.5, d2 = -5.9; ABC obj1(3, 4.4),obj2(3.1, 0.2); swapVals(n1, n2); swapVals(d1, d2); swapVals(obj1, obj2); } We can also use it to swap the values of two objects!!!

What is the problem of the following example? // declare template function template<class T> void showStuff(int stuff1, T stuff2, T stuff3); …… // define the template function template<class T> void showStuff(int stuff1, T stuff2, T stuff3) { cout << stuff1 << endl << stuff2 << endl << stuff3 << endl; } class ABC{ public: ABC(float inx=0, float iny=0) {x=inx; y=iny;} private: float x, y; }; void main() { int n1 = -5, n2 = 0.5; double d1 = -5.5, d2 = -5.9; ABC obj1(3, 4.4),obj2(3.1, 0.2); showStuff(1, n1, n2); showStuff(2, d1, d2); showStuff(3, obj1, obj2); }

What is the problem of the following example? // declare template function template<class T> void showStuff(int stuff1, T stuff2, T stuff3); …… // define the template function template<class T> void showStuff(int stuff1, T stuff2, T stuff3) { cout << stuff1 << endl << stuff2 << endl << stuff3 << endl; } class ABC{ public: ABC(float inx=0, float iny=0) {x=inx; y=iny;} private: float x, y; }; void main() { int n1 = -5, n2 = 0.5; double d1 = -5.5, d2 = -5.9; ABC obj1(3, 4.4),obj2(3.1, 0.2); showStuff(1, n1, n2); showStuff(2, d1, d2); showStuff(3, obj1, obj2); } “cout<<“ cannot handle ABC type of objects! Solution?

What is the problem of the following example? // declare template function template<class T> void showStuff(int stuff1, T stuff2, T stuff3); …… // define the template function template<class T> void showStuff(int stuff1, T stuff2, T stuff3) { cout << stuff1 << endl << stuff2 << endl << stuff3 << endl; } class ABC{ public: ABC(float inx=0, float iny=0) {x=inx; y=iny;} friend ostream& operator<<(ostream& os, ABC& obj); private: float x, y; }; void main() { int n1 = -5, n2 = 0.5; double d1 = -5.5, d2 = -5.9; ABC obj1(3, 4.4),obj2(3.1, 0.2); showStuff(1, n1, n2); showStuff(2, d1, d2); showStuff(3, obj1, obj2); } “cout<<“ cannot handle ABC type of objects! Solution? Define an overload operator function! ostream& operator<<(ostream& os, ABC& obj) { os<<“x=“<<x<<“, y=“<<y<<endl; return os; }

Multiple Type Parameters Can have: template<class T1, class T2> Not typical Usually only need one "replaceable" type Cannot have "unused" template parameters Each must be "used" in definition Error otherwise!

What is the problem of the following template functions? template<class T1, class T2> T1 max (T1 num1, T1 num1) { return num1 > num2 ? num1 : num2; } T2 min (T2 num1, T2 num2) return num1 > num2 ? num2 : num1;

What is the problem of the following template functions? template<class T1, class T2> T1 max (T1 num1, T1 num1) { return num1 > num2 ? num1 : num2; } T2 min (T2 num1, T2 num2) return num1 > num2 ? num2 : num1; T2 is not used!

What is the problem of the following template functions? template<class T1, class T2> T1 max (T1 num1, T1 num1) { return num1 > num2 ? num1 : num2; } T2 min (T2 num1, T2 num2) return num1 > num2 ? num2 : num1; T2 is not used! Missing a template prefix!

What is the output of the following program? #include <iostream> using namespace std; template<class T> T max(T x, T y) { return x > y? x : y; } void main() { int n1 = -5, n2 = 0; double d1 = -5.5, d2 = -5.9; cout << max(n1, d2) << endl; cout << max(d1, n2) << endl;

What is the output of the following program? #include <iostream> using namespace std; template<class T> T max(T x, T y) { return x > y? x : y; } void main() { int n1 = -5, n2 = 0; double d1 = -5.5, d2 = -5.9; cout << max(n1, d2) << endl; cout << max(d1, n2) << endl; Compiling error! T ambiguous… How to fix it?

Class Templates We can also define template class, which can be considered as the “generalized” classes (i.e., can be used to generate actual classes in run-time). template<class T> class Pair { public: Pair(); Pair(T firstVal, T secondVal); void setFirst(T newVal); void setSecond(T newVal); T getFirst() const; T getSecond() const; private: T first; T second; };

Class Templates We can also define template class, which can be considered as the “generalized” classes (i.e., can be used to generate actual classes in run-time). template<class T> class Pair { public: Pair(); Pair(T firstVal, T secondVal); void setFirst(T newVal); void setSecond(T newVal); T getFirst() const; T getSecond() const; private: T first; T second; }; template prefix declaration of the template class

Class Templates We can also define template class, which can be considered as the “generalized” classes (i.e., can be used to generate actual classes in run-time). template<class T> Pair<T>::Pair(T firstVal, T secondVal) { first = firstVal; second = secondVal; } template<class T> void Pair<T>::setFirst(T newVal) { first = newVal; } template prefix definition of the member functions of the template class Need to repeat this prefix for each member function Class name before:: is “Pair<T>”!

Class Templates How to use the template class? create a class with T = int Pair<int> score; Pair<char> seats; class Pair { public: Pair(); Pair(int firstVal, int secondVal); void setFirst(int newVal); void setSecond(int newVal); int getFirst() const; int getSecond() const; private: int first; int second; }; create a class with T = char

Class Templates Use the template class as the parameter of a function int addUP(const Pair<int>& the_Pair); create a class with T = int class Pair { public: Pair(); Pair(int firstVal, int secondVal); void setFirst(int newVal); void setSecond(int newVal); int getFirst() const; int getSecond() const; private: int first; int second; }; Overall, template types can be used anywhere standard types can

Class Templates We can further define the following function as a template to avoid defining new overload. int addUP(const Pair<int>& the Pair); char addUP(const Pair<char>& the Pair); float addUP(const Pair<float>& the Pair); …… How?

Class Templates We can further define the following function as a template to avoid defining new overload. int addUP(const Pair<int>& the_Pair); char addUP(const Pair<char>& the_Pair); float addUP(const Pair<float>& the_Pair); …… template<class T> T addUp(const Pair<T> & the_pair) { return the_pair.first + the_pair.second; }

Class Templates We can further define the following function as a template to avoid defining new overload. int addUP(const Pair<int>& the_Pair); char addUP(const Pair<char>& the_Pair); float addUP(const Pair<float>& the_Pair); …… Template<class T> T addUp(const Pair<T> & the_pair) { return the_pair.first + the_pair.second; } Of course, the “+” operator should apply to the summation of T type of objects!

Another example What is the problem of this class template? class calc { public: int multiply(int x, int y); int add(int x, int y); }; int calc::multiply(int x, int y) { return x*y; } int calc::add(int x, int y) { return x+y; } template<class T> class calc { public: T multiply(T x, T y); T add(T x, T y); }; T calc::multiply(T x, T y) { return x*y; } T calc::add(T x, T y) { return x+y; } What is the problem of this class template?

Another example should be What is the problem of this class template? class calc { public: int multiply(int x, int y); int add(int x, int y); }; int calc::multiply(int x, int y) { return x*y; } int calc::add(int x, int y) { return x+y; } template<class T> class calc { public: T multiply(T x, T y); T add(T x, T y); }; T calc::multiply(T x, T y) { return x*y; } T calc::add(T x, T y) { return x+y; } should be calc<T> What is the problem of this class template?

Another example What is the problem of this class template? template<class T> class calc { public: T multiply(T x, T y); T add(T x, T y); }; T calc<T>::multiply(T x, T y) { return x*y; } T calc<T>::add(T x, T y) { return x+y; } class calc { public: int multiply(int x, int y); int add(int x, int y); }; int calc::multiply(int x, int y) { return x*y; } int calc::add(int x, int y) { return x+y; } What is the problem of this class template?

Restrictions on Type Parameter Only "reasonable" types can be substituted for T Consider: Assignment operator must be "well-behaved" Copy constructor must also work If T involves pointers, then destructor must be suitable! Similar issues as function templates

Type Definitions Can define new "class type name" To represent specialized class template name Example: typedef Pair<int> PairOfInt; Name "PairOfInt" now used to declare objects of type Pair<int>: PairOfInt pair1, pair2; Equivalent to: Pair<int> pair1, pair2; Name can also be used as parameter, or anywhere else type name allowed void fun (PairOfInt &obj);

Friends and Templates Friend functions can be used with template classes Same as with ordinary classes Simply requires type parameter where appropriate Very common to have friends of template classes Especially for operator overloads (as we’ve seen)

Templates and Inheritance Nothing new here Derived template classes Can derive from template or non-template class Derived class is then naturally a template class Syntax same as ordinary class derived from ordinary class

template <class T> class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) { /* do something for foo */ } T Foo_T; // either a TypeA or a TypeB - TBD foo_arg_t _foo_arg; }; template <class T> class Bar : public Foo<T> { public: Bar (const foo_arg_t bar_arg, const a_arg_t a_arg) : Foo<T>(bar_arg) // base-class initializer Foo<T>::Foo_T = T(a_arg); } Bar (const foo_arg_t bar_arg, const b_arg_t b_arg) : Foo<T>(bar_arg) Foo<T>::Foo_T = T(b_arg); void BarFunc (); };

Summary Function templates Class templates Define functions with parameter for a type Class templates Define class with parameter for subparts of class Predefined vector and basic_string classes are template classes Can define template class derived from a template base class

More Compiler Complications Check your compiler’s specific requirements Some need to set special options Some require special order of arrangement of template definitions vs. other file items Most usable template program layout: Template definition in same file it’s used Ensure template definition precedes all uses Can #include it