I worked on my code a bit from the earlier post and was able to make it work. I am new to C++ so please be kind.
I basically have two questions apart from the peer review.
When I use templates definitions in a header and just call it from source, it doesn't work. Why?
To make it work using strings I want to use a
typeof()
alternative of C++. Can anyone suggest one?
The code might be a mess but I would still like some criticism.
#include<iostream>
#include<string>
using namespace std;
template<class T,int size=2>
class stack{
private:
T stacked[size];
int count;
public:
void Insert(T data);
void Display();
stack();
};
template<class T,int size>
stack<T,size>::stack()
{
for(int i=0;i<size;i++)
stacked[i]=0;
count = 0;
}
template<class T,int size>
void stack<T,size>::Insert(T data)
{ if(count>=size)
{
cout<<"Stack is full";
return;
}
count++;
stacked[count] = data;
return;
}
template<class T,int size>
void stack<T,size>::Display()
{ cout<<"Printing out values:"<<endl;
for(int i=0;i<size;i++)
cout<< stacked[i]<<endl;
}
int main()
{
stack<int,5> S1;
S1.Insert(10);
S1.Insert(22);
S1.Insert(5522);
S1.Display();
stack<double,6> S2;
S2.Insert(333);
S2.Insert(6666);
S2.Insert(667777);
S2.Display();
stack<char,6> s3;
s3.Insert('s');
s3.Insert('a');
s3.Insert('v');
s3.Insert('v');
s3.Insert('y');
s3.Display();
/* stack<string,6> s4;
s4.Insert("sav");
s4.Insert("vvy");
s4.Insert("Is a");
s4.Insert("great");
s4.Display();
*/
return 0;
}