
A template is a cookie-cutter that specifies how to cut cookies that all look pretty much the same (although the cookies can be made of various kinds of dough, they’ll all have the same basic shape). In the same way, a class template is a cookie cutter for a description of how to build a family of classes that all look basically the same, and a function template describes how to build a family of similar looking functions.
This is an introduction to templates in C. Basically there are 2 types of templates:
In this section function templates would be discussed.
"Inheritance and composition provide a way to reuse object code. The template feature in C++ provides a way to reuse source code"
In this video, you would learn syntax of function template along with an alternative to the program written in the previous topic with the function overloading.
Here is important note:
In the video you may notice a minor difference between narration and code. Since max is a predefined template in the library, the name of the function template and template functions have been replaced by maxarg.
The video explains mysterious operations taken place under the hood.
It shows also how instantiation is performed by the compiler.
Function template can take parameterized type as well as non-parametarized type as arguments. Here is a video to explain the same.
When you call a function template, the compiler tries to deduce the template type. Most of the time it can do that successfully, but every once in a while you may want to help the compiler deduce the right type — either because it cannot deduce the type at all, or perhaps because it would deduce the wrong type.
For example, you might be calling a function template that doesn’t have any parameters of its template argument types, or you might want to force the compiler to do certain promotions on the arguments before selecting the correct function template. In these cases you’ll need to explicitly tell the compiler which instantiation of the function template should be called.
The same is explained here in the video
All programs in this lecture are compiled using old C compiler. The intention is to exhibit the journey of templates from to old versions to current versions.
If you are not interested in the history, you can straightaway skip this lecture and move to next lecture. The learning process won't be hampered.
If you want to peep inside the history, just sit and enjoy listening and viewing programs.
Interesting set of rule based on earlier matching rules covered in function overloading section in this course earlier
You will learn the method to write multiple templates as a parameter
In this concluding lecture of section 1, you would learn how to create an instance of a template taking instance of the class as an argument. A simple program is intentionally taken to revise many concepts of C++ in a single program.
Here is a video to explain class templates in C++
Here is a class template with its syntax, comparison with function template, instantiation along with 2 sample programs.
Both programs explained in the video have been provided in the resource section of this lecture.
Don't copy-paste them but write on your own using the compiler of your choice.
Like in case of function template, need arises in case of member function of class template to specialize it. The earlier program here is modified in this video with the aid of copy constructor, friend function and template syntax.
"A template is literally a template; a class template is not a class, it's a recipe for creating a new class for each T we encounter"
...Anonymous
Class templates are different from Dynamic binding because class template is the process taken at compile time where as later is at run time. So while dividing the project into source file and header files, it is necessary to write member functions of the class template in the same header file if class template is written in the separate file.
The same is explained here in the video. Study carefully. Type programs as directed in the video and try to understand the process.
It's interesting.
Here is a canonical example of stack class template.
You would understand this program only if you are well versed with stack data structure.
Templates may be used in many combinations with Inheritance
Here is a simple program to understand the methodology of inheriting a class template from another class template.
For example, suppose you have class template with a generic type for a data member. The class has the usual constructor functions and an overloaded operator plus function that adds two instances of itself together.
Then you wish to derive a new class with the added capability to display the data member. This is how the program would appear.
The source has been provided in the resources section.
Like usual class in C++, class template can have static data member. Here is the lecture to discuss intricacies of static data member and its behavior.
Here is a program to create a class template from a non-template class. The source code is made available for downloading in the resources section.
The video covers two aspects of class templates.
Source codes have been made available for downloading.
Type codes and explore on your own. It's interesting mechanism.
The video summarizes contents covered in this course "Demystifying templates in C++". I would be glad if you write a review considering course contents, ease of understanding, methodology, animation and ideas.
Most C++ programmers stay away from C++ templates due to their perplexed nature. The excuses against templates:
As a result, beginning and advanced C++ programmers alike are finding themselves wrestling with templates, attempting to decide why their
code is handled unexpectedly.
Although templates have been part of C++ for well over two decade, they still lead to misunderstanding, misuse, or controversy. At the same time, they are increasingly found to be powerful instruments for the development of cleaner, faster, and smarter software. Indeed, templates have become the cornerstone of several new C++ programming paradigms.
This course would demystify most of the concepts using animation