Developer Data Modeling Mistakes: From Postgres to NoSQL
C++ Template
1. 2011
Saket Kr. Pathak
Software Developer
3D Graphics
Fundamentals Of Programming C++
A quick review of fundamentals of C with an prospective view of implementation in daily day to
day life.
2. Fundamentals Of Programming C++
heiii ...:) what a visualization of template in real life haa haa :). Yes, it's true. Let's think
what we expect from a friend, who makes everything easy within the life, with just a cute
smiley face, who helps to make the life's situations easier to move forward within the
span.
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 become
wife, mom, mother-in-law etc.) who returns directly whatever she got, good or bad. As
here we have the template function as "gal" which returns the type of situation all she
got 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 whatever
she got and returns a smile in her face and type-cast the return value to good
situations/time. Here we have lovng_gal template function with multiple type
parameter.
template <class good_time, class bad_time>good_time
lvng_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. 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 deals
everyone she faces. As in form of template-class. Here gal the template class keeps the
members of different groups in a combined unit as family.
template <class member> //iTempl - friends_group || ipair - gal
class 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. 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 result
in command prompt ... through “printf(…)”:). This template class gal have different type
of parameter as professional_life class and family_life class. The implementation we
have 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_life
nam_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. 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 the
template class gal has another specification for the parametric data type i.e. double
and she knows how to increment it. That’s the smart sense of humor in my loving gal
template class and implemented as;
template <class good_time> //iTempl - good_time
class 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
7. Fundamentals Of Programming C++
}
Hii all, here I had done it and found a bit emotional essence regarding gals within C++
syntax :). Whatever it's a mistake :), and I don’t know how I slipped into this but if you
will try to feel it, I can bet you never forget concepts of Template in your life. Although
any one reads it or not is hardly matters for me, because I do this for my pleasure, I try
to view the concept within society, culture and philosophy. After all "Kurt
Koffka" says: Philosophy is the mother of all sciences.
Take care ... in someone's 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.htm
http://personal.stevens.edu/~wwang3/C++%20Templates%20Tutorial.pdf
Saket Kr. Pathak Page 7