A Wrapper Class

avarionist 0 Tallied Votes 121 Views Share

its a wrapper class.
it combines stl stack class with a custom user class.
i made it so that i could rename push and pop if i wanted or size and top :)
best of all it combines stack and classes :)
something i made to see if i could make it.
any better corrections or solutions would be really appreciated.

#include <iostream>
#include <stack>
#include <string>
using namespace std;
class test{
    public:
    stack<string> item;
    void push(string *ps){
        item.push(*ps);
    }
    void pop(){
        item.pop();
    }
    string top(){
        item.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();
    }
}
thelamb 163 Posting Pro in Training

A few things:

Why is 'item' in the test class public?
In your wrapper class you perform some operations twice:

string top(){
    item.top();
    return item.top();
}
// Why not:
string top(){
    return item.top();
}

Also, your push takes a string* as arugment, but it is not clear if 'push' is actually going to change something to the argument.

void push(string *ps){
    // It is possible to change the string here, for example:
    *ps = "Haha I changed your string!";
    item.push(*ps);
}

The user of your class normally has no idea how you implement your push, so a better approach is:

void push( const string& ps ){
        ps = "Haha I changed your string"; // <-- ERROR! ps is a const&
        item.push(ps);
    }

I assume you made it a pointer so that the string is not copied all the time, but if you use a reference(&) there also will be no copy.

avarionist 1 Junior Poster in Training

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();
    }
}
thelamb 163 Posting Pro in Training

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 ;)

avarionist 1 Junior Poster in Training

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.