System ProgrammingLearn about String Class in C++ Programming

Learn about String Class in C++ Programming

C++(20)-Strings---(4)

In the previous articles, we’ve discussed Strings in detail. We learnt that C/C++ had a classical implementation for strings as arrays of characters. This classical method is called C-Strings. Starting with C++ 98, C++ introduced the standard string class. This class has very useful features that facilitate our work as developers. Learning about this class, and its features is going to be the subject of this article. Wish you a nice reading.

The string Class

The string class is part of the std namespace.

So, what exactly is namespace?

A namespace is a named section of a C++ source file. Within that section, you can define variables, structures, functions, classes, and whatever constructs you need. To refer to either of these things from outside the namespace, the variable name (or function name, class name, etc.) should be preceded with the name of the namespace, followed by the scope resolution operator :: using the following syntax:

NAMESPACE::CLASSNAME

Where NAMESPACE is the name of the namespace, and CLASSNAME is the name of a class defined inside the name space that we need to used from outside.

So, to use the string class, it should be referenced to as std::string.

– Well, but what are the benefits of using the string class?! In other words, you have been teaching us C-Strings in the past articles, and now we are comfortable with it, so why should I learn that string class?

Well, the benefits of using the string class are:

  • It is safer.
  • It is more efficient.
  • It takes over the responsibility of sizing and memory management.
  • It has a list of useful functions and overloaded operators.
  • It is more compliant with the increased trend to use OOP.

Note
A good alternate to writing the namespace (and the :: operator) before using anything defined inside, it is to use the using namespace directive in the beginning of your program. For example, to inform your program to use names from the std namespace, insert the following directive after the #include directives:

using namespace std;

Example
Let’s write a simple program to train on both using the Dev-C++ and the string class.

First, open the Dev-C++. From the menu bar, click File à New à
Project…
1

In the New Project dialog box:

  • Make sure C++ is selected.
  • Select Console Application.
  • Enter the project name (or leave the name suggested by the wizard).
  • Click OK.

2

In the Save As dialog box, click Save.
3

An empty C++ source file is open for editing.
4

Type the following code:

#include <iostream>
#include <conio.h>
using namespace std;
int main() {
	string player1="Alessandro Del Piero";
	string player2("Fernando Redondo");
	cout << "\nMy favorite football players are " << player1 
	     << " and " << player2;
	getch();		
	return 0;
}

Learn the Basics of C Programming Language

Save the source file (File à Save):
5

Now, compile our code:
6

If it goes without errors, the Dev-C++ will create the following files for you:
7

Now, run the program. You can do either from the Dev-C++ (Execute à Run), or by double-clicking the created exe file (Project1.exe). Either way, it should provide the following output:
8

Now, let’s explain things:

  • The directive
using namespace std;

Tells the compiler that names (variable names, function names, class names, etc.) in the following code are part of the std namespace.

  • The two statements:
string player1="Alessandro Del Piero";
string player2("Fernando Redondo");

The present two forms to initialize a new string object. The first form deals with the string object as if it were a normal variable, and assigns a string of characters to it using the ordinary assignment statement. The other form deals with the object as an object of a class and initializes it accordingly.

Summary

In this article, we have discussed the standard string class.

  • A namespace is a named section of a C++ source file.
  • To use a variable, class, or function defined inside a namespace section, the name of the variable (or class, or function) should be preceded by the namespace followed by the :: operator. A short alternative is to use the using namespace directive in the beginning of your program.
  • The string class is safer and more efficient than the ordinary C-Strings. Besides, it offers a list of useful function and operators.

In the next article, we will continue with the string class. We will see more examples that illustrate how useful it could be. See you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -