Programming & Scripting Tutorials

C++: The Basics

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


So to start C++ programming, you’re going to need some kind of IDE (Integrated Development Environment) in which you can write and then compile your code (to compile code means to turn it into something that your computer can understand). I’d suggest using the latest version of Visual C++ Express (at the time of writing this, 2010 (although I personally prefer using 2008)) because it’s free, and it works well (and by many is considered the standard). You can download Visual C++ 2010 Express, here.


Our first C++ program

Once you’ve downloaded and installed (just follow the installer instructions) Visual C++ Express (or any other compiler/IDE that you want to use) – open it up so we can get started. Create a new C++ console project (.cpp file) and delete any code which is generated for you – we are now going to start creating our "Hello World" application.

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 compiler completely ignores (and so it doesn’t affect how your application actually runs). We can do single-line comments using a double slash (//) and multi-line comments using /* to start the comment, and */ to end the comment. For example:

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

After this comment at the top (which isn’t necessary to make the program work since the comments aren’t read by the compiler), we write our ‘includes’. These are basically just pieces of code that are in other files – these are various pieces of code built into C++, and we can include these without having to write out all of the code that does the things. For example, to deal with input and output we need to include ‘iostream’ (input output stream) which is built in.

We tell the compiler that we want to include something by typing a hash sign (#) and then the keyword include, and then we type the name of the thing we want to include (in either triangular brackets or double quotes – don’t worry about this right now). In our example, we want the following so we can output things to the console screen:

#include <iostream>

You usually have include statements at the top of the program so that everything below in your application can use the stuff in the include file.


Next, we’re going to set our namespace to standard (std) – don’t worry too much about what these are yet, but we need this so that we can just tell the program that we want to output (or get) data instead of having to specify that we want the output (or receiving) or data to use the standard namespace each time (by prefixing it with std::). To tell it what namespace we want to use, we use the keyword using followed by the thing we want to use (namespace) and then in this case, the namespace name (std). So we’ll want to add this line next:

using namespace std;

Notice that we finished off the line with a semicolon. Most of the time in C++ we need to end lines with a semicolon (;) to tell the compiler that we’re finished with this instruction or line. In this case, we’re telling the compiler that we’re finished specifying the namespace.

Now we’ve finished our bits at the top, we’re going to create what’s called a "main function". A function is just something that contains some code, and the main function is where your program will start. So if we tell our program to output some text, the first thing in the main function then our program would output this text as the first thing it does. To create the main function, we first have to specify the type of thing it returns. We’ll learn more about this when we learn how to create our own functions, but for the main function we just want it to be an int (this means that at the end of our main function, we need to write the return keyword followed by a whole number (integer) of some kind). After this we have a space and then the name of our function (main in this case), then we have some empty brackets (() (you’ll also understand this more when you learn about functions)) and then we open and close curly brackets. Anything inside these curly brackets is in the main function:

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

Inside of our main function, we’re actually going to do the outputting of data (this is what uses the iostream include). We do this using the cout command (pronounced "see-out"), followed by what are called ‘insertation operators’ (<<) and then the data we want to output. If you want to output a string (a combination or letters (characters) or words), then you have to surround it in double quotes. We then end the line with a semicolon:

int main()
{
	cout << "This is being outputted!";
}

Note that if we want to make the line end after this text (insert a new line character) then you add another set of insertion operators (to show that we want to output more data) and then write endl (which stands for end line).

int main()
{
	cout << "This is being outputted!" << endl;
}

If we ran the program now (by pressing the "Debug" button) then it would work (although it would be bad coding because we haven’t returned a value in the main function) however it would instantly close. The main function has been completed and so it automatically close the program. To stop this happening (so we can actually read the output) we can use the cin keyword (pronounced "see-in") which is for taking input from the user (it uses iostream) and then we add .ignore and then some empty brackets – this tells it to ignore any input taken by the user. We do this so that when we hit enter, it will close the program (so we can choose when to end the program rather than it just killing itself). After this, we add our return as mentioned earlier.

The completed code for our "Hello World" application is as follows:

#include <iostream>

using namespace std;

int main()
{
	cout << "This is being outputted!" << endl;
	cin.ignore();
	return 0;
}
Download

Now that the we’ve finished coding the application – you can simply press the "Debug" button to see your application run!


Variables and User Input (cin)

Now that we can output things to the screen, it’s very useful to also be able to take the data from the user. Before we take any input though – we need a place where we can store that input. A variable is simply a value that can change (think of it as a box which only takes a certain type of things – we can see what’s inside the box, and we can change what’s inside the box) and it can store what the user is inputting. We can create variables by specifying the variable type (in this case we just want to take a whole number from the user, so the int (integer) type will do the job – remember that this means that the user will only be able to input whole numbers into our program because our ‘box’ can only hold whole numbers) followed by the variable name and then putting a semicolon.

Let’s just add the variable declaration (the proper name for the creation of a variable) into the program we had before, and let’s call it 'age':

#include <iostream>

using namespace std;

int main()
{
	int age;
	cout << "This is being outputted!" << endl;
	cin.ignore();
	return 0;
}

As you may have guessed, this program is going to get the users age and then output it back to them! So before we use cin to get data from our user, let’s output some text that prompts them to enter their age by using cout. In this case, we can just change the text from the cout in our last program:

#include <iostream>

using namespace std;

int main()
{
	int age;
	cout << "Please enter your age: ";
	cin.ignore();
	return 0;
}

Notice that I removed the endl because I want the user to type right after the last part of the string that was outputted.

We get the users input by typing cin followed by an extraction operator (just like the insertation operator, but with greater than signs rather than less than signs: >>), followed by the name of the variable that we want to store in input in (in our case – ‘age’):

#include <iostream>

using namespace std;

int main()
{
	int age;
	cout << "Please enter your age: ";
	cin >> age;
	cin.ignore();
	return 0;
}

Ok, so by this point in the program – the variable ‘age’ will have whatever age the user enters in. Well actually, it won’t. We need to change cin.ignore() as it doesn’t function properly when we are using cin earlier in the program. For now, let’s just use system("PAUSE") – it only works on Windows and isn’t a brilliant solution as it uses system calls, but it will do for now:

#include <iostream>

using namespace std;

int main()
{
	int age;
	cout << "Please enter your age: ";
	cin >> age;
	system("PAUSE");
	return 0;
}

Ok, so now the variable age should have the user’s age in it. Outputting it back to the user is very simple – we just use cout, specify the string we want to output before their age after a set of insertation operators, and then specify the variable name after another set of insertation operators (followed by a semicolon). If we wanted, we could even put some more insertation operators on to add some text after the variable!

#include <iostream>

using namespace std;

int main()
{
	int age;
	cout << "Please enter your age: ";
	cin >> age;
	cout << "You are " << age << " years old!" ;
	system("PAUSE");
	return 0;
}
Download

On running the program, you should see that everything actually works! You input your age, and the program spits it back at you!

This C++ tutorial was written by


Back to C++

Advertisement: