Thursday, 31 May 2012

DOWNLOAD TEMPLATESTACK.CPP PROGRAM FILE HERE


File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction



VISIT SITE LIFEQUEST.COM

Hi friends,

Below example program illustrate the fundamentals and advantages of using template in C++ : A generic programming approach

--->If you know the concept of classes and functions that it is not hard for you to understand the concept template.

for example consider to create stack class for basic operation then we simply just one class for any type of data that required for use. But think of  some  time when we want to use stack multiple data types at same time in some application then we need to separately define stack class for each data types. this is increase burden of programming, but if we use template class then we need not define separate class for each data type we want to use and further no need of any inheritance, let's look how it works.

Here  i have created stack class that i can use for any data types i want, mechanisam behind this is simple create template class that accepts parameters as any data types that we want to use in creation of object of
stack class.

Look below at syntax:


template <class type>
class class-name

in the above two lines three words in the orange colors are keywords that we use for template class declaration, in green color the word 'type' is kind of parameter for data type  that is replaced by actual data type when we instantiate an object of template class  and class is the class name for template class.

Now have a look at some code in program and try to understand it:

Template class stack:


template <class x>
class stack
{
x stck[SIZE];
int top;
public:
stack()
{
top=0;

}
x pop();
void push(x);


};



Now you can see first two line in orange are same as we have discussed above in syntax. then inside < >
are keyword class and 'x' is the parameter for data type. the body of class is not hard to under stand there is only declaration of stack operation function such as push,pop and one default constructor.

Now outside the class are function definition of push and pop operations.


PUSH:

template <class x>void stack<x> :: push(x ob)

{
if(top==SIZE)
{
cout<<"stack is full \n";
return;
}
stck[top] = ob;
top++;
}

pop:

template <class x>  x stack<x> :: pop()

{
if(top==0)
{
cout<<"Stack is empty. \n";
return 0;
}
top--;
return stck[top];
}

now in the both above class same use of scope resolution  operator as we use with common classes. 
Green words are the class name and  orange is the return type of function.

Now let look at main program :

int main()
{
int ch;
while(1)
{
clrscr();
cout<<"----------------------------"<<endl;
cout<<"-------Template stack-------"<<endl;
cout<<"-1.create  stack of integer-"<<endl;
cout<<"-2.create stack of float----"<<endl;
cout<<"-3.create stack of double---"<<endl;
cout<<"-4.create stack of character"<<endl;
cout<<"-5.Exit--"<<endl;


cout<<"-Enter your choice----------"<<endl;
cin>>ch;
switch(ch)
{

case 1:

stack<int> s1,s2;
int i;
cout<<"Stack of Integer:"<<endl;
s1.push(1);
s2.push(10);
s1.push(2);
s2.push(20);
s1.push(3);
s2.push(30);

for(i=0;i<3;i++)
cout<<"Pop S1 :"<<s1.pop()<<"\n";

for(i=0;i<3;i++)
cout<<"Pop S2 :"<<s2.pop()<<"\n";
break;
case 2:
stack<float> fs1,fs2;
cout<<"Stack of float:"<<endl;
fs1.push(1.1);
fs2.push(21.2);
fs1.push(3.3);
fs2.push(21.4);
fs1.push(5.5);
fs2.push(21.6);

for(i=0;i<3;i++)
cout<<"Pop ds1 : "<<fs1.pop()<<"\n";
for(i=0;i<3;i++)
cout<<"Pop ds2 : "<<fs2.pop()<<"\n";
break;
case 3:
stack<double> ds1,ds2;
cout<<"Stack of double:"<<endl;
ds1.push(1.111);
ds2.push(21.211);
ds1.push(3.311);
ds2.push(21.422);
ds1.push(5.533);
ds2.push(21.446);

for(i=0;i<3;i++)
cout<<"Pop ds1 : "<<ds1.pop()<<"\n";
for(i=0;i<3;i++)
cout<<"Pop ds2 : "<<ds2.pop()<<"\n";
break;
case 4:
stack<char> cs1,cs2;
cout<<"Stack of character:"<<endl;
cs1.push('d');
cs2.push('f');
cs1.push('g');
cs2.push('j');
cs1.push('y');
cs2.push('i');

for(i=0;i<3;i++)
cout<<"Pop ds1 : "<<cs1.pop()<<"\n";
for(i=0;i<3;i++)
cout<<"Pop ds2 : "<<cs2.pop()<<"\n";
break;
case 5:
exit(0);
}

cout<<"\n\n\t\tPress any key to continue....."<<endl;
getch();

}
return 0;

      Don't afraid of length of program it's too simple too understand, it is long just b'cos i have created menu driven program.Look at statement in orange colors just below case statements, it is just that as per user requirement we have passed so and so data types as arguments to create objects of that types. 
Is not it easy? 
it is easy Download program from here and run it.

output snap:
Advantages of templates:
--->Using template enable us to create generic classes and functions , hence provide support for generic programming.
--->help to create family of classes and functions.


Visit this link to see program on template functions:
Template Functions in C++


Other such programs:

File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction



VISIT SITE LIFEQUEST.COM