I just wanted some feedback on my C++ implementation of Stack. I know this is somewhat messy but I am trying to improve the way I use classes. I am pretty sure that I am not using templates properly.
Any suggestions are welcome!
#include <iostream>
#include </root/Dev/Stack.h>
#include<string>
using namespace std;
template<class T>
Stack<T>::Stack(T &size)
{
capacity = size;
if(size==0)
{
cout<<"Capacity can't be zero!"<<endl;
stack = 0;
}
else
{
stack = new T[capacity];
}
count = -1;
}
template<class T>
void Stack<T>::Insert(T &data)
{
if (count == (capacity-1))
{
cout<<"Stack is full";
return;
}
else
{
count++;
stack[count] = data;
}
}
template<class T>
void Stack<T>::Display()
{
cout << "Printing out values:" << endl;
for (int i=0; i<count; i++)
cout << stack[i] << endl;
}
int main()
{
Stack<int> S1(5);
S1.Insert(10);
S1.Insert(22);
S1.Insert(5522);
S1.Display();
Stack<string> S2(6);
S2.Insert("hi");
S2.Insert("Savvy");
S2.Insert("How are you?");
S2.Display();
return 0;
}
The header file:
#include<iostream>
using namespace std;
template<class T>
class Stack {
public:
int capacity;
int count;
T *stack;
void Insert(T &num);
void Display();
Stack(T &value);
};