In the first part, [
Strings Part I] we saw what strings were. In this second part, we'll write a small C++ program that uses strings.
All our programs will be created using a simple text editor. In Windows you can use Notepad or install a different text editor. On the Mac and in Linux, I prefer to use VIM.
You'll need a C++ compiler. In Windows, you can download Visual Studio Express Edition from Microsoft's web site. It comes with a command line compiler that you can use. Since I don't want to complicate things by adding notes on what to do, I'll leave this one up to you!
On Linux, and on my Mac, gcc/g++, are easily installed. On both these systems it requires installing developer tools. For the Mac, you only need to install the Xcode developer tools that are supplied
free of charge with every Mac operating system. At least since the OS X flavour of Mac OS.
OK... lots of words, no code yet. So here we go. Create a file and call it
s1.cpp. The contents of s1.cpp are:
File: s1.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World!";
cout << s << endl;
return 0;
}
Explanation:
C++ is a small programming language. It lacks built-in ability to do a number of high level processing tasks automatically. For example, if you have a string "abcd," C++ has no built-in method to let you know how many characters are in that string. You'd have to count them.
But this lack of built-in features is one of C++'s strengths. It allows programmers to add this by writing their own libraries of routines to do this work. These libraries can then be shared. Some of them are even part of the basic C++ language specification now because they are so important.
#include <iostream>
This adds the
iostream library to our program. The iostream library provides functions that allow us to read and write from the console, or command prompt.
#include <string>
This adds the
string library to our program. The string library is the focus of this tutorial series and collects a number of useful features that simplify how C++ handles strings.
using namespace std;
I won't go into namespaces, however, it's important to note that the main idea behind namespaces is to avoid name clashes. For example, if another programmer wrote a library of string functions which had similar ones provided by the <string> library we declared in our program, we'd still be able to use our string library and the one designed by this other programmer. Namespaces will make sure that similar names in both libraries don't cause confusion.
Finally, our program.
int main() {
All our programs will have a
main() function. As the names indicates, this is the entry point to the program. All processing begins here.
string s = "Hello World!";
This line declares a
string object and
initialises it to the
value "Hello World!"
To declare a variable in C++, you begin by writing the variable type name. The variable type name indicates the type of data you want to store. In our case, we declared the name
string to indicate that we would like to store string objects. We gave our string object the name "
s."
cout << s << endl;
This next line uses the function
cout to send, or display, the contents of the string object,
s, to the screen. cout stands for "
console output."
return 0;
This line returns a
status back to the operating system. It's standard to return a value of zero when everything is OK. Returning a value to the operating system is important since it allows programs to be chained together intelligently and larger programs built by using smaller programs. In our case we have to return a value since at the top of the program we wrote -- int main(). This said that we were going to return an
integer, or whole number, value.
Once you've finished writing the program, it's time to compile it and run it. On the Mac, or in Linux, all you do is type in the following command in the same folder location as the source code you just wrote.
This calls the g++ compiler to create the object, or binary, version of the program s1.cpp. The
-o tells the g++ compiler to name the resulting program "
s1."
To execute the program, on a Mac or Linux, type the following:
That's it for this part.