Create a class template for a class named GeneralStackthat holds
In the main() function, create three objects with different data types of class General Stack and test the functionality of member functions for various values of data members for these objects.
#include <iostream>
using namespace std;
template<typename type>
class GeneralStack{
type stack[50];
int size;
public:
GeneralStack(){
for(int i = 0; i < 50; i++) stack[i] = (type) 0;
size = 0;
}
void push(type data){
if(size == 50){
return;
}
else stack[size++] = data;
}
void pop(){
if(size == 0) return;
else{
cout<<stack[--size]<<" ";
stack[size] = (type)0;
}
}
bool currentStatus(){
if(size == 50) return true;
return false;
}
};
int main(){
GeneralStack<int> integers;
GeneralStack<float> floats;
GeneralStack<char> chars;
for(int i = 0; i < 52; i++){
integers.push(i);
floats.push(i);
chars.push(i + 65);
}
if(integers.currentStatus()) cout<<"\nArray is full!\n";
floats.pop();
cout<<endl;
for(int i = 0; i < 45; i++){
integers.pop();
}
if(!chars.currentStatus()) cout<<"\nArray is NOT full";
cout<<endl;
chars.pop();
return 0;
}
Comments
Leave a comment