String Class in C++

String Class in C++ with Examples:

In this article, I am going to discuss String Class in C++ Language with examples. Please read our previous article, where we discussed Built-in String Functions in C++ with examples.

String Class in C++:

This is a built-in class in C++. Already we have learned about string using arrays and pointers but now we will see String Class i.e. a built-in class available in C++. So, here, we will learn what does it mean by class, what does it mean by an object and what are the member functions or what are the operations upon an object. So, for accessing this built-in class we have to include ‘string’ header file i.e. “#include <string>”.

String str;

This is a declaration of a variable of string. This variable is called as an object. So, when we declare a variable of type class, we use the term object and stop calling it as a variable. it’s better to call it as an object. If we want to declare and initialize the string object then we will write,

String str = “Welcome”;

If we initialize this then object string will be created. This will be creating an array internally. We don’t have to declare an array, internally it will create an array and inside that array, the word “Welcome” will be stored letter by letter. Then afterwards null character is also inserted. For better understanding, please have a look at the below image. So, the String class is also having null character terminated or null character delimited.

String Class in C++

What is the size of the string?

Then one more important thing that it has created a bigger string. It will not create an exact size array that is required for a string but it will create some little larger size. So, the array size will be created larger than the string. If your string itself is larger still it will create bigger size array.

It will make sure that there are few more spaces remaining after the string is store. So, it will have some more capacity for storing or expanding the system. So, this arrays size we can say it as capacity of a string and the number of letters that are stored up to null character is called as length of a string or it is also called the size of a string.

So always capacity of array or string object will be bigger than the length of a string. So, what is the idea behind this one? It is like a buffer space or extra space. If we are modifying this string and adding more alphabets to this one, so there is a space already available.

The next thing is supposed already we have a string and we trying to store something bigger than the size of the present capacity of a string. Then what will happen?

What is the size of the string?

It will increase the size of this array internally. So actually, it will create a very bigger size array compare to this one then it will take a fresh array and it will write a new string. So, it will automatically manage the size of the array. Same array cannot be increased remember this. New array has to be created and then this string will be stored.

This class have a built-in mechanism for storing any size of string whatever you are giving. It will increase the capacity to a bigger size and then it will store a new string.

So, the point here is this is a self-manage string so we don’t have to worry about the available spaces and you can draw or reduce the size of a string depending on the data that you are storing or depending on the text that you are storing in string.

cin >> str;

This is a method for reading a string from the keyboard like ‘cin’ is a known object, we have been using it. This is used for getting a stream of characters from the keyboard and it will store it in ‘str’. But the problem with this one is it will take only one word. If you type multiple words, it will not take. Then for reading multiple words, we have a global function available that is ‘getline’.

getline(cin, str);

In this function we can pass stream i.e. ‘cin’ as well as the string object i.e. ‘str’. For displaying a string, we will write,

cout << str;

Now let us look at the program to understand these things in better way.

Program for String Class in C++:
#include <iostream>
#include <string>
using namespace std;
int main()
    string str1, str2;
    cout << "Enter a String:\n";
    getline (cin, str1);
    getline (cin, str2);
    cout << str1 << " " << str2 << endl;

Program for String Class in C++

In the next article, I am going to discuss Basic Functions of String Class in C++ with examples. Here, in this article, I try to explain String Class in C++ Language with examples. I hope you enjoy this Built-in String Class in C++ with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *