Home Page :: Authors Login :: Author Signup :: More Articles
About This Author :: Request Reprint :: Print Article :: Tell A Friend |
|
Templates in C++ Before I start this tutorial I assume that you have a Working Knowledge of OOP in C++. Without this, the tutorial will be of no use to you. This tutorial shows you how to Reuse Code, Save Typing Time, Prevent Time Spent on Debugging and saving source-code space. OK. Let me take a common example. The gr8 min() function returns the value which is lowest from the given two arguments.I shall show two xamples now. One without Templates and one with Templates.Here is the first program. OUTPUT: 5 A 1.91 Function Overloading doesn't help much as far as typing time and source space is concerned.Also if you find an error in one such function,you have to correct it in all the other functions as well.With Templates all the work becomes a lot easier. Let's see the Templated Version now... Same Output. Nearly the same Executable size. But what we have achieved is that the time we take to type the code is reduced. The code looks more readable and incase there is an error just correcting it here will make the change for all data types accessing this function. Let me explain what happens at compile time.When the Function us first called with this: It goes to the function min(). Now T is the value that will be substituted with the data type of the parameter. So here T will be int. So Dynamically the Function Declaration becomes like this... In the second case T takes the form of a char and so on...The Compiler on every call still has to generate seperate functions for each data type but what we are saving are typing time and debugging ease. Some of you might wonder....what if we want all data-types to access a templated function except one? Like for example in a abs() function we would like all data types to access it except a char data-type. What do we do then? Well in such acase we override the function call like this... This way even if accidentaly a char data type is used as a parameter to the abs function..the overloaded function gets called...not the Templated version. What if we wanted a multi-parametered Templated Function? Here is the Syntax You now know enough of Function Templates.Now it's time to move onto Class Templates. OK. Now let us assume that we have a Stack class(Data Structure) that is capable of storing only int's. Now suppose you want it to store float's as well. You would normally write the whole class again by Copy/Pasting the Previous class and make the necessary Changes. But hey,There is a much better alternative. Using templates, you can manage just one class that handles different data-types. That way even if you find an error in your class, fixing it will make changes to all the data-types. Here is an example of a templated stack data structure. Everything looks similiar except an object declaration stack <int> s1; means that the value T is substituted by int. User-Defined Data Types can also be used provided the required overloaded operator functions are present. In case if we define the class functions outside the templated class,the syntax changes a bit.For a Constructor: Yes,we have to define that template thing everytime we write a function outside the class.But if a function returns an object of the same templated class the syntax would be like this: Templates can also have pre-defined values like this: Templates can be inherited as well like this: That's all about templates. If you use Templates Frequently you will save a lot of typing and debugging time. Suggestions\Querys can be brought to my notice at born2c0de@hotmail.com HAVE FUN CODING!!!
Discuss this in our Webmaster Forums About The Author:Sanchit Karve Sanchit Karve is currently a Student in Mumbai who in his spare time devotes himself to programming. He knows quite a lot of languages including C/C++/Assembly/PERL/PASCAL/BASIC/MFCVC++ etc. He loves to teach programming and writes Tutorials/Articles/Code and submits them for people to learn from them.
|
About This Author :: Request Reprint :: Print Article :: Tell A Friend |





