-1

I've recently switched from C to OOP languages - C++ and Python 3.4

I noticed that there are quite-a-lot things about Python that C++ can't beat one of them being the ease of calling functions.

So i've decided to somehow implement the python-way of making function calls in C++, using the STL.

I started off with the 'string' class.

In Python, i can do this:

str="hello world   "
str.strip().split()

and that would first, strip-off the trailing whitespaces at the end of the string and then split the string in 2 halves at at the whitespace

now that's NEAT and i'd love to be able to call functions this way, ie, object.func1().func2().func3() and so on

as i have absolutely no idea about the stl class 'string', i started off with my own made class 'MYstring'

class MYstring
{
    string str;
public:
    //constructor
    MYstring(string str)
    {   setter(str);    }

    //setter function
    MYstring& setter( string str )
    {   this->str=str;
        return *this;
    }

    //getter_str function
    string getter_str()
    {   return str;
    }

    unsigned int getter_size()
    {   return str.size();
    }

    //modifier function(s)
    MYstring& split(char x='\0')
    {   string temp="";
        for(int i=0; i<str.size(); i++)
        {   if(this->str[i-1] == x)
                break;
            temp += str[i];
        }
        this->str=temp;
        return *this;
    }

    MYstring& strip()
    {
        for(int i=this->str.size() - 1; i>=0; i--)
        {   if(this->str[i] == ' ')
                this->str.erase(i);
            else if(isalnum(this->str[i]))
                break;
        }
        return *this;
    }
};

To test the class and its member functions, i used the following main()

int main()
{   //take a user-defined string-type input
    string input;
    cout<<"Enter a string: ";
    getline(cin,input);

    // create an object of class 'MYstring'
    // and initialise it with the input given by the user
    MYstring obj(input);

    //take the character at which the string must be split
    cout<<"\nEnter character to split the string at: ";
    char x;
    cin>>x;

    //display original input
    cout<<"\n\nThe user entered: "<<obj.getter_str();
    cout<<"\nSize = "<<obj.getter_size();

    obj.strip().split(x);//  <--- python style function call
    cout<<"\n\nAfter STRIP and SPLIT: "<<obj.getter_str();
    cout<<"\nSize = "<<obj.getter_size();

    return 0;
}

And It Works

So, finally here's my question:

How can I create and use the same split and strip functions for the C++ STL string class? Is it possible to use our own created methods with the STL?

Every suggestion is welcome.

MistUnleashed
  • 309
  • 1
  • 3
  • 15
zhirzh
  • 3,273
  • 3
  • 25
  • 30

2 Answers2

0

You can extend the class std::string and use the extended class. So your class will have all the functionality of the STL string plus you additional methods.

class mystring: public std::string {
   ...
};
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
0

Based on experience and on the topics covered in the link that starrify posts in the comments, I would not subclass the STL classes but instead create your own class as you've done.

But also, in addition to your std::string copy constructor you might add a typecast-to-std::string operator overload, so it's easy to have code convert to and from your object and std::string when working with libraries that take std::string.

Corbell
  • 1,283
  • 8
  • 15