ŶŴŵŵ

Saket Kr. Pathak
SoItware Developer
3D Graphics

Fundamentals Uf Programming C++
quick review oI Iundamentals oI C with an prospective view oI implementation in daily day to
day liIe.
Funuamentals 0f Piogiamming C++

Saket Ki. Pathak Page Ŷ

IeIII ...:) wIuL u vIsuuIIzuLIon oI LempIuLe In reuI IIIe Iuu Iuu :). Yes, IL's Lrue. ¡eL's LIInk
wIuL we expecL Irom u IrIend, wIo mukes everyLIIng eusy wILIIn LIe IIIe, wILI jusL u cuLe
smIIey Iuce, wIo IeIps Lo muke LIe IIIe's sILuuLIons eusIer Lo move Iorwurd wILIIn LIe
spun.

mmmHHmmm, ¡ LIInk I um dIverLIng Lo LIe wuy oI pIIIosopIy Irom LIe wuy oI C++.
¡eLs come Buck :) und Iuve u sLory we wIII dIscuss IeuLures oI LempIuLe uIong LIe wuy.

Single purumeter Templute Ionction:
A guI wus LIere ("wus" becuuse, IL muy possIbIe, now LIe guI wus noL jusL guI, buL become
wIIe, mom, moLIer-In-Iuw eLc.) wIo reLurns dIrecLIy wIuLever sIe goL, good or bud. As
Iere we Iuve LIe LempIuLe IuncLIon us "guI" wIIcI reLurns LIe Lype oI sILuuLIon uII sIe
goL LIrougI purumeLers.

template <class situation>situation gal(situation good_time,
situation bad_time)
{
return (good_time < bad_time ? bad_time : good_time);
}

oltiple purumeter Templute Ionction:
NexL LIme I Iud seen u compurIson umong sILuuLIons, Iere IovIng guI uccepLs wIuLever
sIe goL und reLurns u smIIe In Ier Iuce und Lype-cusL LIe reLurn vuIue Lo good
sILuuLIonsJLIme. Here we Iuve Iovng_guI LempIuLe IuncLIon wILI muILIpIe Lype
purumeLer.

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 Iuve muIn IuncLIon, us:

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): %d\n", iresult);

Funuamentals 0f Piogiamming C++

Saket Ki. Pathak Page ŷ

lnam_1 = 1020304050;
lnam_2 = lnam_1++;

lresult = lvng_gal<long>(lnam_1, lnam_2);
printf("Greater Number(long): %ld\n", lresult);

lresult = lvng_gal<int,long>(inam_1, lnam_2);
printf("Greater Number(long): %ld", lresult);

getchar();
return 0;
}

Single purumeter Templute Cluss:
AILer Some LIme LIe IovIng guI goL some responsIbIIILIes und sIe comIorLubIy deuIs
everyone sIe Iuces. As In Iorm oI LempIuLe-cIuss. Here guI LIe LempIuLe cIuss keeps LIe
members oI dIIIerenL groups In u combIned unIL us IumIIy.

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;

Funuamentals 0f Piogiamming C++

Saket Ki. Pathak Page Ÿ

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: %f\n", result);

getchar();
return 0;
}

oltiple purumeter Templute Cluss:
Here now LIe IovIng guI munuges Ier IIIe In boLI proIessIonuI us weII us IumIIy concern.
As per prIorILy oI IumIIy, wIo Luke cure oI Ier proIessIonuIIsm Loo, und sIows LIe resuIL
In commund prompL ... LIrougI ¨prInLI(.)¨:). TIIs LempIuLe cIuss guI Iuve dIIIerenL Lype
oI purumeLer us proIessIonuI_IIIe cIuss und IumIIy_IIIe cIuss. TIe ImpIemenLuLIon we
Iuve us;

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()
Funuamentals 0f Piogiamming C++

Saket Ki. Pathak Page Ź

{}
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: %ld\n", result);

getchar();
return 0;
}

Speciulizution within Templute Clusses:
Here LIe IovIng guI Is sIowIng uII smurLness wILI compIeLe InLeIIecLuuI sense und uII,
wIuLever LIe gIILs sIe geLs sIe knows ILs vuIue und respecLs IL In sume munner. As LIe
LempIuLe cIuss guI Ius unoLIer specIIIcuLIon Ior LIe purumeLrIc duLu Lype i.e. dooble
und sIe knows Iow Lo IncremenL IL. TIuL`s LIe smurL sense oI Iumor In my IovIng guI
LempIuLe cIuss und ImpIemenLed us;

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;
}

Funuamentals 0f Piogiamming C++

Saket Ki. Pathak Page ź

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: %d\n", fun.increase());

gal<double> smiles(0.000000f);
printf("Double value: %f\n", smiles.increase());

getchar();
return 0;
Funuamentals 0f Piogiamming C++

Saket Ki. Pathak Page Ż

}

HII uII, Iere ¡ Iud done IL und Iound u bIL emoLIonuI essence regurdIng guIs wILIIn C++
synLux :). WIuLever IL's u mIsLuke :), und ¡ don`L know Iow ¡ sIIpped InLo LIIs buL II you
wIII Lry Lo IeeI IL, ¡ cun beL you never IorgeL concepLs oI TempIuLe In your IIIe. AILIougI
uny one reuds IL or noL Is IurdIy muLLers Ior me, becuuse ¡ do LIIs Ior my pIeusure, ¡ Lry
Lo vIew LIe concepL wILIIn socIeLy, cuILure und pIIIosopIy. AILer uII ort
oIIku suys: !hilosophy is the mother oI ull sciences.

Tuke cure ... In someone's sLyIe ... noL mIne ... ¡ used Lo suy ... cLcI u uguIn ... :)


ReIerences:
ILLp:JJwww.cpIuspIus.comJdocJLuLorIuIJLempIuLesJ
ILLp:JJwww.IIs.sInIcu.edu.LwJ-kuLIyJvcsLIJLempIuLes.ILm
ILLp:JJpersonuI.sLevens.eduJ-wwung¸JC++%zoTempIuLes%zoTuLorIuI.pdI

Sign up to vote on this title
UsefulNot useful