#include<iostream>
#include<conio.h>
using namespace std;
template<class X>
class Stack{
private:
X index;
X pile[100];
public:
/*void initialization(){
index=0;
}*/
/*This function can be put into Constructor so that it initialize itself for the new objects. So, I am putting in it Constructor.*/
Stack():index(0){} /*Default constructor to set the index 0.*/
void push(X temp){
if(index==100){
cout << "\nStack is full.\n";
return;
}
pile[index++]=temp;
}
X pop(){
if(index==0){
cout << "\nStack is empty.\n";
}
return pile[--index];
}
};
int main(){
Stack<int> s1;
/*s1.pop(); You will get the message "Stack is empty." here*/
// s1.initialization();
s1.push(100);
s1.push(20);
s1.push(30);
cout << s1.pop() << endl;
cout << s1.pop() << endl;
cout << s1.pop() << endl;
getch();
return 0;
}
Example of stack using the class template.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.