Generic Specifics
Templates!! Aaah, we have all heard that name. But, have we ever used it? Well, I hadn't until recently, when during work, I was asked to generate a generic piece of code that can manage records. Please NOTE, this is NOT Siemens proprietary and it is just a simple record system asked by my mentor to start off my training skills. But, to be on the safer side, I will not post the code here HEHE!! :D
The objective of the record system is to be as scaleable, reusable as possible. So, obviously, Object Oriented programming is the solution to that problem for it to be reusable and scaleable. Now, how can it be generic enough that the record can manage ANY type of data? HELLO TEMPLATES!!! Never fear, the templates are here. A beautiful concept of generic programming starts with something known as templates. It's a very simple concept as well. There are two types of templates - FUNCTION TEMPLATES AND CLASS TEMPLATES. For the people who are aware of and use STL and MFC Container classes, STL's are THE example of function templates and Container Classes are the example of a class template. So, what is this thingamajig ya call template? Well, lemme elaborate with a simple example - the tried and tested and easy to understand example of a max function!! We have in all probability used the max(x,y) function, where the function will return the larger of x and y. Now, in C++ we used to generate overloaded functions of max(x,y) for the various data types, including user defined types. So what did we do to save time? Copy, Paste, Find, Replace and all the editing works that the editor provides us, and those of us who are seriously demented wrote a piece of code to replicate this and change it!! Hehe. Anyhoo, there is a much simpler way to do this using the concept of function templates. Aaaarrrrgggghhhh!! Ya keep talking about this damn thing - templates, when are you gonna show us how it looks? Well, here it is...
template /*Actually you are supposed to use the jargon "typenames" instead of class, as in template */
So, there it is mates! Now, all you need to do is call the function as you would normally ANY function. Reference it with its paramenters and BOOM, it works. You don't have to overload it for the enormous amounts of primitives.The only thing extra you need to do to make it work with user defined types is to overload the ">" operator and BAM! there ya have it, a perfectly reusable piece of code. Some Interesting things about templates :D!! Well, we know that code reuse is present in C++ through inheritance. When you think about it, some simple differences between ala Template and ala Inheritance. Ze main difference is in how the reuse is happening? Templates are a sorta macro substitution of the typename to generate code for that typename whereas, inheritance is the code reuse across inherited classes. [PLEASE NOTE: It's more complicated than macro substitution and has proficient error checking during compilation]. So mate? Got 'e general 'dea o' 'em 'emplates? k?? Enough of Hagrid talk... let's get on with Class templates, where templates can be used to contain data and attribute and reuse them across the same functionalities across varied data types. :)! As it's gonna take some time for me to code the damn thing and format it in html and I have to add more functionalities to the record system, I was talking about, lemme take leave now. But, to come back and prolly give another quill at class templates. More and more interesting things are taking place with programming. On a high and mighty note, I am being put into development in SISL. It's an amazing place, highly knowleadgeable people, totally friendly and comfortable working environment :)! Until next break... ENJOY!!!\:D/ --- As Always Guru Panguji http://members.lycos.co.uk/gurupanguji/
|
TopicsRecent blog posts
|