////////////////////////////////////////////////////////////////////////////////
#include<iostream.h>
#include<conio.h>
const int MAX=100;
////////////////////////////////////////////////////////////////////////////////
template <class Type>
class stack
{
private:
Type st[MAX]; //declaring array of any type
int top;
public:
stack() //constructor
{
top = -1;
}
void push(Type var) //var is of any type
{
st[++top]=var;
}
Type pop()
{
return st[top--];
}
};
////////////////////////////////////////////////////////////////////////////////
int main()
{
stack <float> s1; //s1 is object of class stack<float>
s1.push(1111.1F);
s1.push(2222.2F);
s1.push(3333.3F);
cout<<"1:"<<s1.pop()<<endl;
cout<<"2:"<<s1.pop()<<endl;
cout<<"3:"<<s1.pop()<<endl;
stack <long> s2; //s2 is object of class stack<long>
s2.push(123123123L);
s2.push(235235235L);
s2.push(345345345L);
cout<<"1:"<<s2.pop()<<endl;
cout<<"2:"<<s2.pop()<<endl;
cout<<"3:"<<s2.pop()<<endl;
getch();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/*The template concept can be extended to classes. Class template is generally used for data storage (container) classes.
Ordinary classes could store data of only a single basic type. But by using class template, it would be possible to
write a single class specification that would work for variables of all types, instead of a single basic type.*/
#include<iostream.h>
#include<conio.h>
const int MAX=100;
////////////////////////////////////////////////////////////////////////////////
template <class Type>
class stack
{
private:
Type st[MAX]; //declaring array of any type
int top;
public:
stack() //constructor
{
top = -1;
}
void push(Type var) //var is of any type
{
st[++top]=var;
}
Type pop()
{
return st[top--];
}
};
////////////////////////////////////////////////////////////////////////////////
int main()
{
stack <float> s1; //s1 is object of class stack<float>
s1.push(1111.1F);
s1.push(2222.2F);
s1.push(3333.3F);
cout<<"1:"<<s1.pop()<<endl;
cout<<"2:"<<s1.pop()<<endl;
cout<<"3:"<<s1.pop()<<endl;
stack <long> s2; //s2 is object of class stack<long>
s2.push(123123123L);
s2.push(235235235L);
s2.push(345345345L);
cout<<"1:"<<s2.pop()<<endl;
cout<<"2:"<<s2.pop()<<endl;
cout<<"3:"<<s2.pop()<<endl;
getch();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/*The template concept can be extended to classes. Class template is generally used for data storage (container) classes.
Ordinary classes could store data of only a single basic type. But by using class template, it would be possible to
write a single class specification that would work for variables of all types, instead of a single basic type.*/
0 comments:
Post a Comment