ok thanks for the feedback i revised it here is the new code along with an example of it in use. like i said just for fun no harm no foul :)
also code tested and works fine under Gcc 4.3.
i use code::blocks ide as well under linux.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
class test{
public:
stack<string> item;
void push(string *ps){
/* void push takes a string pointer as an arguement because the Stl implementation requires a arguement(or so i believe(almost all examples i have seen pass an arguement to stl member .push();*/
item.push(*ps);
}
void pop(){
item.pop();
}
string top(){
return item.top();
}
int size(){
item.size();
return item.size();
}
};
int main(){
test me;
int loop;
string input;
for(int loop=0; loop<5;loop++){
cout<<"Enter some text: ";
cin>>input;
me.push(&input);
}
while(me.size()>0){
input=me.top();
cout<<input<<endl;
me.pop();
}
}
avarionist
Junior Poster in Training
70 posts since May 2010
Reputation Points: 11
Solved Threads: 3
Ok you've missed 2 of my points:
1 - Why is item public? You create a wrapper to perform actions on the item(push, pop etc.) but since item is public, it is also possible for code to directly access it (e.g. me.input.pop()), that defeats the point of having this wrapper class.
2 - I guess you didnt understand what I meant with the comment about push...
You comment that all the STL implementations take an argument to push(), which is absolutely correct. With my comments I did not tell you to remove this argument, I suggested to change it to make the behavior of your push() function more clear to a user.
I suggest you read up on references in C++ and if you don't understand why push should take a 'const string&' rather than a 'string*' come and ask what exactly you don't understand ;)
ok now i understand you better ill go read up on reference's i normally just use pointers so.... lol im such a nub
avarionist
Junior Poster in Training
70 posts since May 2010
Reputation Points: 11
Solved Threads: 3