C++ Template


Published on

An quick overview of Template in C++ with a bit flavor of emotion and a light sketch of humanity in it.

Published in: Technology, Self Improvement
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C++ Template

  1. 1. 2011Saket Kr. PathakSoftware Developer3D GraphicsFundamentals Of Programming C++A quick review of fundamentals of C with an prospective view of implementation in daily day today life.
  2. 2. Fundamentals Of Programming C++heiii ...:) what a visualization of template in real life haa haa :). Yes, its true. Lets thinkwhat we expect from a friend, who makes everything easy within the life, with just a cutesmiley face, who helps to make the lifes situations easier to move forward within thespan.mmmHHmmm, I think i am diverting to the way of philosophy from the way of C++.Lets come Back :) and have a story we will discuss features of template along the way.  Single parameter Template Function:A gal was there ("was" because, it may possible, now the gal was not just gal, but becomewife, mom, mother-in-law etc.) who returns directly whatever she got, good or bad. Ashere we have the template function as "gal" which returns the type of situation all shegot through parameters.template <class situation>situation gal(situation good_time,situation bad_time){return (good_time < bad_time ? bad_time : good_time);}  Multiple parameter Template Function:Next time i had seen a comparison among situations, here loving gal accepts whatevershe got and returns a smile in her face and type-cast the return value to goodsituations/time. Here we have lovng_gal template function with multiple typeparameter.template <class good_time, class bad_time>good_timelvng_gal(good_time temp_1, bad_time temp_2){return (temp_1 < temp_2 ? temp_2 : temp_1);}Now we have main function, as:int main(int argc, _TCHAR* argv[]){ int inam_1, inam_2, iresult; long lnam_1, lnam_2, lresult; inam_1 = 3; inam_2 = inam_1++; iresult = gal<int>(inam_1, inam_2); printf("Greater Number(int): %dn", iresult);Saket Kr. Pathak Page 2
  3. 3. Fundamentals Of Programming C++ lnam_1 = 1020304050; lnam_2 = lnam_1++; lresult = lvng_gal<long>(lnam_1, lnam_2); printf("Greater Number(long): %ldn", lresult); lresult = lvng_gal<int,long>(inam_1, lnam_2); printf("Greater Number(long): %ld", lresult); getchar(); return 0;}  Single parameter Template Class:After Some time the loving gal got some responsibilities and she comfortably dealseveryone she faces. As in form of template-class. Here gal the template class keeps themembers of different groups in a combined unit as family.template <class member> //iTempl - friends_group || ipair - galclass gal{public: member family[2]; gal(member nam_1, member nam_2); virtual ~gal(); member getMax();};template <class member>gal<member>::gal(member nam_1, member nam_2){ family[0] = nam_1; family[1] = nam_2;}template <class member>gal<member>::~gal(){}template <class member>member gal<member>::getMax(){ member result;Saket Kr. Pathak Page 3
  4. 4. Fundamentals Of Programming C++ result = family[0]<family[1] ? family[1] : family[0]; return result;}int main(int argc, _TCHAR* argv[]){ gal<double> work_time(120.0123, 102.3201); double result = work_time.getMax(); printf("This is greater: %fn", result); getchar(); return 0;}  Multiple parameter Template Class:Here now the loving gal manages her life in both professional as well as family concern.As per priority of family, who take care of her professionalism too, and shows the resultin command prompt ... through “printf(…)”:). This template class gal have different typeof parameter as professional_life class and family_life class. The implementation wehave as;template <class professional_life, class family_life>class gal{public: professional_life iCollect_1; family_life iCollect_2; gal(professional_life nam_1, family_life nam_2); virtual ~gal(); family_life getMax_diff();};template <class professional_life, class family_life>gal<professional_life, family_life>::gal(professional_lifenam_1, family_life nam_2){ iCollect_1 = nam_1; iCollect_2 = nam_2;}template <class professional_life, class family_life>gal<professional_life, family_life>::~gal()Saket Kr. Pathak Page 4
  5. 5. Fundamentals Of Programming C++{}template <class professional_life, class family_life>family_life gal<professional_life, family_life>::getMax_diff(){ family_life result; result = iCollect_1<iCollect_2 ? iCollect_2 : iCollect_1; return result;}int main(int argc, _TCHAR* argv[]){ gal<int,long> laugh(120, 10203040); long result = laugh.getMax_diff(); printf("This is greater: %ldn", result); getchar(); return 0;}  Specialization within Template Classes:Here the loving gal is showing all smartness with complete intellectual sense and all,whatever the gifts she gets she knows its value and respects it in same manner. As thetemplate class gal has another specification for the parametric data type i.e. doubleand she knows how to increment it. That’s the smart sense of humor in my loving galtemplate class  and implemented as;template <class good_time> //iTempl - good_timeclass gal{public: good_time elem; gal(good_time iVal); virtual ~gal(); good_time increase();};template <class good_time>gal<good_time>::gal(good_time iVal){ elem = iVal;}Saket Kr. Pathak Page 5
  6. 6. Fundamentals Of Programming C++template <class good_time>gal<good_time>::~gal(){}template <class good_time>good_time gal<good_time>::increase(){ return ++elem;}template <>class gal<double>{public: double elem; gal(double iVal); virtual ~gal(); double increase();};gal<double>::gal(double iVal){ elem = iVal;}gal<double>::~gal(){}double gal<double>::increase(){ elem = elem + 0.000001f; return elem;}int main(int argc, _TCHAR* argv[]){ gal<int> fun(3); printf("Int value: %dn", fun.increase()); gal<double> smiles(0.000000f); printf("Double value: %fn", smiles.increase()); getchar(); return 0;Saket Kr. Pathak Page 6
  7. 7. Fundamentals Of Programming C++}Hii all, here I had done it and found a bit emotional essence regarding gals within C++syntax :). Whatever its a mistake :), and I don’t know how I slipped into this but if youwill try to feel it, I can bet you never forget concepts of Template in your life. Althoughany one reads it or not is hardly matters for me, because I do this for my pleasure, I tryto view the concept within society, culture and philosophy. After all "KurtKoffka" says: Philosophy is the mother of all sciences.Take care ... in someones style ... not mine ... I used to say ... ctch u again ... :)References:http://www.cplusplus.com/doc/tutorial/templates/http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htmhttp://personal.stevens.edu/~wwang3/C++%20Templates%20Tutorial.pdfSaket Kr. Pathak Page 7