C++: The Basics

C++ (pronounced "see-plus-plus") is an object oriented, general purpose programming language that was created in 1983 by Bjarne Stroustrup. It's used mainly for desktop software and game development, and is an extremely useful programming language to know.

So to start C++ programming, you'll need some way to write your C++ code, and then also a compiler which turns that code into something that your computer can directly read (often called "machine language"). If you're using the Mac OS X operating system or a Linux distribution, I'd recommend using any text editor of your choice to write your code (I particularly like Sublime Text 2), and then using the 'g++' compiler to compile your code via the Terminal. For (Mac) OS X, g++ can be very easily installed as it comes bundled along with XCode.

Another popular choice for C++ coding and compilation is using an Integrated Development Environment (IDE) in which you can write and compile your code. For OS X you might want to use XCode if you're feeling worried about the g++ route (g++ can be a little complicated), and if you're using Windows, popular IDEs include Code::Blocks, which I personally recommend however does require some setup at the start (you may need to do some research), and Visual C++ Express, which is much easier to set up, however will require some sort of application pausing before the end of the 'main' function in most cases as it generates a window which will disappear after execution. If you decide to go with Visual C++ Express, just keep this in mind -- you may wish to put the system("PAUSE"); or _getch(); lines before the end of your 'main' function (the latter of which requires you to #include <conio.h>) -- if you don't understand what this means, you will soon enough.

So by this point, you should have your development environment properly set up (even if you don't entirely know how to use it yet). If you're using an IDE, create a new C++ project, often called a "console project", and remove any code/text which is generated for you, and if you're using a separate text editor and compiler, just create a new file with the '.cpp' extension (the file extension for C++ source files) - I'm going to name mine 'project.cpp'.

To start off with, we're just going to add what's called a 'comment' to the top of our file which says that we've written the program. Comments are simply notes that you write (to either yourself, your team, or whoever else might see the code) that the C++ compiler completely ignores, and so these don't affect how your application actually runs. We can do single-line comments in C++ by using a double slash (//) and multi-line comments using /* to start the comment, and */ to end the comment. Take, for example, the following:

1
//This is a single-line comment and this program was created by Joe Savage.
1
2
3
4
5
/*
This is a multi-line comment.
It can have multiple lines!
This program was created by Joe Savage.
*/

After a comment on your top line (which isn't entirely necessary), we need to "include" a few things before we begin writing our main application logic. When we "include" things in C++, we're simply taking pieces of pre-written code from a file - as such, includes are usually done at the start of a section of code so that the functionality can be used in the rest of the code. In this case we want to include the "iostream" file (which stands for input output stream) so we can output and take in basic text data. These includes always start with a hash symbol (#), and then use the include keyword followed by the name of the file we wish to include (which we specify, in this case, in angle brackets):

1
#include <iostream>

As alluded to earlier, the stuff inside 'iostream' is for inputting and outputting text. The two most well-known things in here are called cout and cin (pronounced "see-out" and "see-in"), which are for outputting data and getting data from the user. These both, however, require the "std" namespace - I'm not going to go into great detail about namespaces right now, but essentially just think about different namespaces as different drawers. A lot of the core C++ standard stuff is in the "std" drawer and we can specify this by either writing std:: before every time we write cout, cin, and other things which require it, or for our simple purposes here, we can just use the namespace throughout out whole document. This is done by writing the using keyword, followed by the namespace keyword, followed by the namespace name, in this case, std:

1
using namespace std;

Notice that I also finished the line by using a semicolon (;) - lines are often finished with a semicolon in C++ to show that the instruction or the line has finished. In this case, we're telling the compiler that we're done specifying the namespace by writing the semicolon.

With the top part of our document setup, we can actually begin writing our program's point of entry. All basic C++ programs start at what is called the "main" function. A function is just a piece of code with a name, and that name, in this case, is "main". Functions also have a type, generally speaking the "main" function should have the integer (whole number) type, and this is shown by writing the int keyword before the function name. This function type means that inside the function we must have a line which returns an integer value - don't worry too much about what this means right now, but if we return '0' from main, it generally means that we did everything without any errors. After we've written the function's name, we have to specify some brackets (which some people populate with some things, but we're going to leave them empty), and then some curly brackets which will actually contain our main application code. For now, I'm just going to put a comment in there. Our code so far should look as follows:

1
2
3
4
5
6
7
8
9
//Created by Joe Savage of Dev-HQ.net
#include <iostream>

using namespace std;

int main()
{
	//The stuff inside here is going to be in the main function
}

Notice that I put a tab-space before the comment - this is because the comment is inside the "main" function and I wanted to visually show this. Indented code isn't necessary for your code to compile and run correctly, however it makes finding errors a hell of a lot easier, and if you don't do it, nobody will like you (or want to work with you!).

While we're talking about the "main" function itself, let's put in that 'return' line we talked about earlier so that our main function abides to its type. This is done by simply writing the return keyword followed by the value which you want to return, followed by a semicolon - so in this case, return 0;. This will actually stop our "main" function's execution though, so we want it to be the very last line of the function:

1
2
3
4
5
int main()
{
	//The stuff inside here is going to be in the main function
	return 0;
}

From here, we're going to complete our basic program by simply getting it to output "Hello World!" (or text of your choice) to the screen. As alluded to earlier, this is done by using cout (which, remember, is in the std namespace). We can output a combination of different letters, numbers, and symbols, known as a string, by writing the cout keyword, followed by insertion operators, <<, followed by our string which is represented by wrapping in double quotes. We also need a semicolon to end the line.

1
2
3
4
5
int main()
{
	cout << "Hello World!";
	return 0;
}

The way cout works, is essentially that you can just put insertion operators after each piece of data you want to output to output some more. Numbers don't have to be surrounded in double quotes to be processed in the correct way be the compiler, so if we wanted to output a number after our "Hello World!" piece of text, we could either put it inside our string, which would encapsulate the data, or we could put it after another set of insertion operators (and move the semicolon to the "new" end of the line):

1
2
3
4
5
int main()
{
	cout << "Hello World! " << 9001;
	return 0;
}

You should try to get used to this functionality. Without looking at the snippet below which reveals the answer, try to create a basic cout which would output a sentence with a number in the middle.

1
2
3
4
5
int main()
{
	cout << "Hello World! I am " << 32 << " years old!";
	return 0;
}

We can add newlines to this output by either writing a backslash followed by the letter 'n' in a string (remember that strings are encapsulated by double quotes), or by using the endl keyword after some insertion operators. So our final piece of code which we're going to compile and run (which I've commented a bit to make it easier to understand), is as follows:

1
2
3
4
5
6
7
8
9
10
//Created by Joe Savage of Dev-HQ.net
#include <iostream> //Input/output stream

using namespace std; //'std' namespace for 'cout' and 'endl'

int main() //Our main function
{
	cout << "Hello World! I am " << 32 << " years old!" << endl; //Output some text
	return 0; //Return 0
}

To compile and run the code in an IDE, you can usually just hit a conveniently placed "Run" or "Debug" button (Key shortcuts of F8 in Code::Blocks, F5 in Visual C++ Express, and +R in XCode), however the process of compiling and running is slightly more confusing when you have a separate text editor and compiler. If this is the case, save the file with the code we've written (and with the '.cpp' file extension, so call it something like 'project.cpp'), and then open up a Terminal window (if you don't know how to do this in your OS, Google it). You can then navigate to wherever your project file (i.e. 'project.cpp') is stored using the 'cd' command (again, you'll have to Google if you don't know how to do this), and then run some compilation and running commands which are compiler and OS-specific. If you're using g++, you can run g++ -o project project.cpp to compile the code in "project.cpp" to a file named "project", and then on OS X and Linux you can run ./project to run the generated "project" file (and hence the program) itself.

In future tutorials I will expect you to know how to compile and run code in your own development environment, so it'd probably be a good idea for you to create a few C++ projects and compile/run them to get used to your development environment. As a little challenge to tie off the information in this tutorial (which will, of course, be built upon in future tutorials), try writing another cout line after the one we've already written that outputs something else.