+ All Categories
Home > Documents > Templates in C++ (Part 2) - S.M. Riazul Islam,...

Templates in C++ (Part 2) - S.M. Riazul Islam,...

Date post: 20-May-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
21
Templates in C++ (Part 2)
Transcript
Page 1: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Templates in C++(Part 2)

Page 2: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Contents

• Stack Implementation Using Class Templates

• Passing Class Template Objects to Function Templates

• Templates and Friends

• Templates and Inheritance

Page 3: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Implementation Using Class Templates

• Stack: a data structure into which we insert elements at the top and retrieve those items in Last-In-First-Out (LIFO) principle.

• However, to instantiate a stack, a data type must be specified.

• Templates: to make it independent of the type of the items being placed in the stack.

Page 4: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Using Templates: Stack Operations

Page 5: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Using Templates

Stack.h(1/2)

Page 6: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Using Templates

Stack.h(2/2)

Page 7: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Using Templates

Main.CPP(1/2)

Page 8: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Using Templates

Main.CPP(2/2)

Page 9: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Stack Using Templates

Page 10: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Passing a Stack template object to a function template

Page 11: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

StackTemplateObject to Function

Page 12: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Passing a Stack template object to a function template

Page 13: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Non-Type Parameters and Default Types for Class Templates

• It’s also possible to use non-type template parameters, which can have default arguments and are treated as consts

Page 14: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Class Template and Class

• The declaration MyClassTemplate<int> is a class, or a class based on a template.

• There are no special properties of a class based on a template vs. a class not based on a template.

• The special properties are of the template itself.

Classes do not define templates, templates define classes

Page 15: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Templates and friends

• It is known to us that functions and entire classes can be declared as friends of nontemplate classes.

• With class templates friendship can be established

between a class template and a global function, a member function of another class(possibly a class-template specialization), or even an entire class (possibly a class-template specialization).

Page 16: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Templates and friends

Page 17: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Templates and friends

Page 18: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Templates Exercise 1

Templated friend function of a templated class

• Write a program to test the given approach for establishing the friendship between the template class and the friend function

Do you find any bad thing in this approach?

Page 19: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Templates and Inheritance

• A class template can be derived from a class-template specialization.

• A class template can be derived from a nontemplate class.

• A class-template specialization can be derived from a class-template specialization.

• A nontemplate class can be derived from a class-template specialization.

Page 20: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Template and Static Members

• Nontemplate class: One copy of each static data member is shared among all objects of the class, and the static data member must be initialized at global namespace scope.

• Each class-template specialization instantiated from a class template has its own copy of each static data member of the class template; all objects of that specialization share that one static data member.

Page 21: Templates in C++ (Part 2) - S.M. Riazul Islam, PhDlecture.riazulislam.com/uploads/3/9/8/5/3985970/slides... · 2019-11-04 · Templates and friends • It is known to us that functions

Review: C++ Standard Library Class Template Vector

(Lecture Slides 7)


Recommended