Simple string class

I am trying to make some simple string class (yust for practice). I have some doubts at begining so i need to see original c++ sting class definition(but dont know how to see it since it is integrated in language) and if u have some simple string classes plz show me.
Thanks! :)
Go find <string> and read that (have fun though...)
Here is a simple string class.
http://cplusplus.com/reference/string/string/

Personally, I think that the question about writing a string class is inappropriate for a beginners forum. This is not a task that a beginner should be trying to do. It is too complicated. Some of the smartest programmers and engineers in the industry spent years developing the std libraries so that they are typesafe, secure, efficient, and easy to use. There is no way a beginner is going to write something better than a std::basic_string. if you are going to write your own, why bother studying the original? Just use it as is. Now if you want to extend it, that is a different story but certainly not a beginner topic. You will learn the language a lot faster by studying the documentation for existing containers and algorithms and learning how to use those tools as is.
This is not a task that a beginner should be trying to do
The OP is asking just for hints of a simple string class, that isn't much difficult
kempofighter
I disagree. Learning how to manage data in a class is very appropriate to beginners learning C++. A simple string class doesn't have to be as perfect as the STL (heck, a lot of STL implementations still in use are horribly bad programming). It just has to work.

pajo
You are jumping-in over your head to play with the STL. Try writing your own stuff first. A good start is to have a class with the following three data members and the following five member functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
class mystring
  {
  char*    data;  // The actual C-string data, allocated via new()
  unsigned size;  // The length of the data[] array
  unsigned used;  // The number of characters used in the data[] array
  ...
  public:
    mystring() ...
    mystring( const char* s ) ...
    mystring( const mystring& s ) ...
    mystring& operator = ( const mystring& s ) ...
   ~mystring() ...
  };

After that, you can add stuff to handle operators, string comparisons, etc.

Hope this helps get you started.
Last edited on
Duoas
I totally agree with u. As u said i am not trying to program string to be as perfect as STL, but some basic like u started in example. Ye i started like that and i wanted like that! :) I had doubt with initializacion (overloping operator =) and data members, but when i searched <string> i fund my answers.
Thx all :)
I guess we'll just have to respectfully disagree. Attemping to write code to learn how classes work is one thing. Writing a class that does with the STL already provides is another beast altogether. You'll probably end up teaching yourself really bad habits and end up with faulty assumptions about how the STL actually does work. As you learn to program, of course you will have to learn how to manage data inside a class. That's kind of a no brainer. that doesn't mean you have to write a string. Is writing a string class the only way of teaching yourself how to manage data inside a class? You could write a class called "animal", "polygon", "shape", or "cat" to teach yourself that. When you start writing classes, as a beginner, called string or vector, or dynamic_array, you are setting yourself up for failure.

If you wanted to build something with lego pieces would you buy the material for the plastic, melt it yourself, and shape it into the building blocks first? I doubt it. you'd buy a kit and learn how to use the existing building blocks.
Two very interesting opinions and reads from you both.


If you wanted to build something with lego pieces would you buy the material for the plastic, melt it yourself, and shape it into the building blocks first? I doubt it. you'd buy a kit and learn how to use the existing building blocks.


Have to say i do like that analogy though :)
kf, you are comparing apples to oranges.

It doesn't matter what the class is: animal, polygon, string... whatever. It is learning to manage your data that counts -- whatever that data may be. The wider the variety of data, the more you learn.

Further, there is nothing provably objectionable about wanting to play with strings and string classes. Just because it is in the STL doesn't mean it is perfect, or that it cannot be improved upon, or even that writing your own simple version is tantamount to reinventing the wheel. It is good to invent your own wheel every now and then -- you learn something more about wheels.

Claiming that bad habits and faulty assumptions will occur is a slippery-slope fallacy -- a very obvious one that you should have caught -- so please don't patronize me with high language. I've studied a lot of pedagogy, and a vast amount of computer sciences.

In computers, the building blocks are whatever you want them to be. Don't cut people down for hack.
I would like to say that in response to this:

You'll probably end up teaching yourself really bad habits and end up with faulty assumptions about how the STL actually does work.


I don't really see how building stuff that exists the STL would cause this any more then just building your own random classes anyway, or how it would give you any assumptions on how the STL works.
IMO writing a string or a vector class is better than writing a rectangle or animal class as with strings or vectors you can practice many things: classes, templates and dynamic allocation, with shape or animal you can practice only OOP
Topic archived. No new replies allowed.