How to Create a Dynamic String in C++

Save

Dynamic data types do not have a fixed size. When a computer stores a number, it uses the same number of bytes to store the number "0" and the number "20000." But try to store a sentence and the panorama changes dramatically. Since each sentence has a different length, the memory used to store the sentence has to change dynamically. Standard C did not include a string data type, and designing a dynamic string data type (or string class) has become almost a rite of passage of any new C/C++ programmer.

Things You'll Need

  • Computer
  • C++ Compiler (recommended: Visual C++ Express)

Design a String Class

  • Visualize the features of your string class. Your string must have the following:

    Dynamic size (changes with user input, or assignment).

    Be able to know the size of your string.

    Output the string stored in memory.

    Store the size.

    Store the first character of our string (since it is dynamic, you do not know where it ends.)

  • Separate which features of your string class are information (or data) and which ones are functions. Usually, anything that involves action is a function. From our list:

    Dynamic size -> Function based on how do you construct a string.

    Know the size of the string -> Function.

    Output the string -> Function.

    Store the size -> data/information.

    Store the beginning of the string -> data/information.

  • Define the role of each function, and the type of data members of your class in simple terms. From the previous list:

    Dynamic Size - Function - Constructs the string.

    Know the size - Function - Returns string size.

    Output the string - Function - Returns the string contents.

    Store the size - data/information - must be a positive integer.

    Store the beginning of the string - data/information - a character.

Implementing the String Class - Header File

  • Create a header file called mystring.h (File ->Add -> Header File). A header file is like an index for your code.

  • Define your class in the header file. This is the code:

    class mystring{

  • Create public functions to address all the functions in your design. This is the code:

    public:

    mystring(); //creates an empty string

    mystring( const char*); //creates and initializes the string to some value (in the parenthesis)

    ~mystring(); //deallocates dynamic memory resources

    int getsize(); //shows us the size of the string

    char* outputmystring(); //outputs the string

  • Create private data members to store the data of your string. This is the code:

    private:

    int _size; //stores the string size

    char _mystr; /this is a data member that points to the first character of the string. If we know the first character and the size of the string, we can access all its characters */

    }

  • Type your complete header file code (you can copy and paste from here):

    / mystring.h /

    class mystring{

    public:

    mystring();

    mystring( const char*);

    ~mystring();

    int getsize();

    char* outputmystring();

    private:

    int _size;

    char *_mystr;

    }

Implementing the String Class - Code File

  • Create a matching CPP file called mystring.cpp (File->Add -> cpp file - empty file). Here, you will include the definitions for the function on the mystring.h header file. Just like a book, header files store the main tittles, and cpp files store the contents and definitions of each tittle.

  • Include the cstring and iostream libraries. This is the code:

    / mystring.cpp /

    include <cstring>
    include <iostream>
  • Define the construction functions (empty construction and parameter construction). Here is the code:

    mystring::mystring() //Empty constructor

    {

    _size=0;

    _mystr=0;

    }

    mystring::mystring( const char str) /constructs and initializes string to a value*/

    {

    if ( ! str)

    { _size=0; _mystr=0;} / if no value, string is empty /

    else

    { _size = strlen( str );

    _string = new char[ _size +1 ]; /this is our dynamic memory allocation, based on the size of the parameter/

    strcopy( _string, str);

    }

    }

  • Define the destructor function. This function is used to de-allocate the dynamic memory. Every time the memory used changes, the computer must know that it can use that part of the memory for other purposes; this process is called de-allocation. The code:

    mystring::~mystring

    { delete [] _mystr; } /deallocates our dynamic memory /

  • Define the function to return the string size and output the string. The size is straightforward. Just display the value of _size:

    int mystring::getsize() { return ( _size ); }

    To output the string, we will output each character until we reach the end of the string (which happens to be the value of _size):

    void mystring::outputmystring()

    { for(int count=0 ; count == size ; count++)

    { cout << _mystr[count];}

    cout << endl; }

  • Here is your complete code for mystring.cpp:

    / mystring.cpp /

    include <cstring>
    include <iostream>

    mystring::mystring() //Empty constructor

    {

    _size=0;

    _mystr=0;

    }

    mystring::mystring( const char str) /constructs and initializes string to a value*/

    {

    if ( ! str)

    { _size=0; _mystr=0;} / if no value, string is empty /

    else

    { _size = strlen( str );

    _string = new char[ _size +1 ]; /this is our dynamic memory allocation, based on the size of the parameter/

    strcopy( _string, str);

    }

    }

    mystring::~mystring

    { delete [] _mystr; } /deallocates our dynamic memory /

    int mystring::getsize() { return ( _size ); }

    void mystring::outputmystring()

    { for(int count=0 ; count == size ; count++)

    { cout << _mystr[count];}

    cout << endl; }

Tips & Warnings

  • You can always enhance your mystring class by adding functions and operators. Be careful when typing, even small mistakes can cause errors that are very difficult to trace.

References

  • Photo Credit Comstock/Comstock/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!