Disclaimer: This tutorial assumes you understand the basics of how a computer compiles it’s code.
Most programming books start with a lecture on the history of programming, how computers work, and why you should learn to program. Out of these three subjects, I’m assuming you already know the answer to the third (“Why you should learn to program”), considering that you’re reading a book about programming. We will go over how computers work, however the only history we will be covering is what will make you a better programmer. So, without further ado, let’s jump right in to the basic history of C++.
You may have heard that C++ is superset of C, and this is partially true. C++ was started by Bjarne Stroustrup more than thirty years ago. It all started with Simula.
Bjarne was doing work on his P.h.d. Thesis, and he had the opportunity to work with the Simula programming language. Simula was the first language to support Object-Oriented Programming. Bjarne thought this method of programming was smart, however Simula was far too slow for practical use.
Thus, Bjarne began work on “C with Classes”. He added Object-Oriented Programming, strong type checking, inlining, and default function arguments (On top of inheritance and all the features of the C language.) As they say, the rest is history.
The most recent update to C++ was C++11. C++11 added many new features to C++, increasing performance and readability of code by a strong factor. Noticeable improvements we’ll see in the second lesson will be the auto variable and the constexpr (Constant Expression).
Now we will go over getting a C++ editor / compiler. First, go to Microsoft's Visual Studio Page, click on the “Download” button located near the bottom of the page, and then scroll down to Visual Studio 2010 Express Products. Expand the Visual C++ 2010 Express tab, select your language, and download.
Visual Studio comes with it’s own installer, so simply run the downloaded file with recommended settings to install Visual Studio.
Now comes the fun part: Our first program. Open Visual Studio and click on the the “File” button in the top-left hand corner. Hover over the “New” button, and click on “New Project”. You will now be greeted with a window containing many different options for types of projects. Select the “Win32 Console Application” option and name your project “Hello World!”.
On the next page to open, click finish without changing anything. You will now be shown the basic code of a project. You should see:
Also, remove the line which says "#include "stdafx.h"". Remember how when we were setting up our project we unclicked the "Precompiled headers" check-box? "#include "stdafx.h"" is our Precompiled Headers file. Since we're using a small project, we don't need Precompiled headers, so we delete the line. (If you don't understand Precompiled headers or why they're important, you can simply ignore it now. They only become important when working on large applications where it can take a long time to compile.)
Your code should now look like:
Now, type out (don’t copy and paste):
Let’s look at what the above code does, character by character:
The # sign is a signal to the preprocessor. The preprocessor runs through your code before actually compiling it and checks for lines which start with the # sign. This sign means the word(s) after it are commands for the preprocessor.
You may be wondering: What is a pre-processor? Well, a pre-processor is like a Video-Game tester. These testers go through before the game is released (compiled) and fix any bugs (They actually just report the bugs, however you get the idea). The pre-processor hunts through your source code before compiling for the # sign, and then executes the command(s) following the # sign. Normally, these commands involve printing out source code.
The above command (#include) tells the preprocessor to include the following file. The filename is located between either a less than and greater than sign (<) or two quotes (“”). We’ll explore the difference between the < and “” signs once we start using more than one file, however for now we will use <.
Including the file means that the preprocessor hunts for the file, and if it finds the file, it will print out everything in the file where you put the #include statement. This means that both the code located inside the included file and your code are compiled.
In this case, we #include <iostream>;. Iostream is part of the standard-library of C++, and it stands for “input-output stream”. Iostream is normally used for input and output to the console. This means we can print out words and data to the console, and get user-input from the console.
Your code should now look like this:
When we included iostream, we were including a part of the standard-library (std). To access commands in the standard-library, we use the scope-resolution operator (::). The lets us access code in namespaces. A namespace is like a package in Java, it groups together code. The scope-resolution operator tells the compiler that the code we're accessing is in a specific namespace.
Now for the last two lines of code we will change in Visual Studio. After the first bracket ({), however before the return 0; line, type in:
The “cout” command is part of the std (standard-library) namespace. It prints to the console what we insert into it. When we included iostream (iostream is used for console input and output), we included the “cout” command.
The scope-resolution operator (::) lets the compiler know that cout is part of the standard-library(std). After cout, we see the left-shift operator (<) This operator inserts the code following it into “cout”. In this case, we insert “Hello World!”. If we are inserting different types of variables or commands into “cout”, we separate them with the left-shift operator (<). In this case, we also want to use endl (Another part of the standard library). Endl and hello world are completely different (Hello World! is a string, endl ends the current line in the console), so we use the left-shift operator (<) to separate them. Endl stands for End Line, and it does exactly that.
At this point, you’re probably wondering why we have semicolons. Semicolons tell the compiler where statements end. This whole program could be on a single line, however we use whitespace (space which contains no code) to make the code easier to read. To have all the code be on a single line, we would simply need to write it all on the same line, separating our commands / statements using semicolons. You put semicolons at the end of your commands / statements, signifying that another command / statement is coming after that semicolon.
Next, we will examine cin.get();. This command tells the console to wait for user input.
Now we can finally run our code. Simply press “F5” on your keyboard (or select “start debugging” from the visual studio debug menu) to run the code. You will probably be met with a message about your code being “out of date”. This is a normal Visual Studio warning, so simply click “yes” to continue running your program. If you get any errors, here is the full code:
Cheers :)!
What does #include <filename> do?
What is the preprocessor?
What does cin.get() do?
What does cout do?
What does the insertion operator do?
What does endl do?
Most programming books start with a lecture on the history of programming, how computers work, and why you should learn to program. Out of these three subjects, I’m assuming you already know the answer to the third (“Why you should learn to program”), considering that you’re reading a book about programming. We will go over how computers work, however the only history we will be covering is what will make you a better programmer. So, without further ado, let’s jump right in to the basic history of C++.
You may have heard that C++ is superset of C, and this is partially true. C++ was started by Bjarne Stroustrup more than thirty years ago. It all started with Simula.
Bjarne was doing work on his P.h.d. Thesis, and he had the opportunity to work with the Simula programming language. Simula was the first language to support Object-Oriented Programming. Bjarne thought this method of programming was smart, however Simula was far too slow for practical use.
Thus, Bjarne began work on “C with Classes”. He added Object-Oriented Programming, strong type checking, inlining, and default function arguments (On top of inheritance and all the features of the C language.) As they say, the rest is history.
The most recent update to C++ was C++11. C++11 added many new features to C++, increasing performance and readability of code by a strong factor. Noticeable improvements we’ll see in the second lesson will be the auto variable and the constexpr (Constant Expression).
Now we will go over getting a C++ editor / compiler. First, go to Microsoft's Visual Studio Page, click on the “Download” button located near the bottom of the page, and then scroll down to Visual Studio 2010 Express Products. Expand the Visual C++ 2010 Express tab, select your language, and download.
Visual Studio comes with it’s own installer, so simply run the downloaded file with recommended settings to install Visual Studio.
Now comes the fun part: Our first program. Open Visual Studio and click on the the “File” button in the top-left hand corner. Hover over the “New” button, and click on “New Project”. You will now be greeted with a window containing many different options for types of projects. Select the “Win32 Console Application” option and name your project “Hello World!”.
On the next page to open, click finish without changing anything. You will now be shown the basic code of a project. You should see:
// Hello World!.cpp : Defines the entry point for the console application. // #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; }Before we continue, we must change the line which says "int _tmain(int argc, _TCHAR* argv[])" to simply say "int main()". "int main()" is a function. In later tutorials we will learn more about the main function (and what a function is), however now I will simply say that the main function contains the code that our application runs (it is the entry-point for our application).
Also, remove the line which says "#include "stdafx.h"". Remember how when we were setting up our project we unclicked the "Precompiled headers" check-box? "#include "stdafx.h"" is our Precompiled Headers file. Since we're using a small project, we don't need Precompiled headers, so we delete the line. (If you don't understand Precompiled headers or why they're important, you can simply ignore it now. They only become important when working on large applications where it can take a long time to compile.)
Your code should now look like:
// Hello World!.cpp : Defines the entry point for the console application. // int main() { return 0; }
Now, type out (don’t copy and paste):
#include <iostream>right after the two lines which start with "//".
Let’s look at what the above code does, character by character:
The # sign is a signal to the preprocessor. The preprocessor runs through your code before actually compiling it and checks for lines which start with the # sign. This sign means the word(s) after it are commands for the preprocessor.
You may be wondering: What is a pre-processor? Well, a pre-processor is like a Video-Game tester. These testers go through before the game is released (compiled) and fix any bugs (They actually just report the bugs, however you get the idea). The pre-processor hunts through your source code before compiling for the # sign, and then executes the command(s) following the # sign. Normally, these commands involve printing out source code.
The above command (#include) tells the preprocessor to include the following file. The filename is located between either a less than and greater than sign (<) or two quotes (“”). We’ll explore the difference between the < and “” signs once we start using more than one file, however for now we will use <.
Including the file means that the preprocessor hunts for the file, and if it finds the file, it will print out everything in the file where you put the #include statement. This means that both the code located inside the included file and your code are compiled.
In this case, we #include <iostream>;. Iostream is part of the standard-library of C++, and it stands for “input-output stream”. Iostream is normally used for input and output to the console. This means we can print out words and data to the console, and get user-input from the console.
Your code should now look like this:
// Hello World!.cpp : Defines the entry point for the console application. // #include <iostream> int main() { return 0; }
When we included iostream, we were including a part of the standard-library (std). To access commands in the standard-library, we use the scope-resolution operator (::). The lets us access code in namespaces. A namespace is like a package in Java, it groups together code. The scope-resolution operator tells the compiler that the code we're accessing is in a specific namespace.
Now for the last two lines of code we will change in Visual Studio. After the first bracket ({), however before the return 0; line, type in:
std::cout << "Hello World!" << std::endl; std::cin.get();
The “cout” command is part of the std (standard-library) namespace. It prints to the console what we insert into it. When we included iostream (iostream is used for console input and output), we included the “cout” command.
The scope-resolution operator (::) lets the compiler know that cout is part of the standard-library(std). After cout, we see the left-shift operator (<) This operator inserts the code following it into “cout”. In this case, we insert “Hello World!”. If we are inserting different types of variables or commands into “cout”, we separate them with the left-shift operator (<). In this case, we also want to use endl (Another part of the standard library). Endl and hello world are completely different (Hello World! is a string, endl ends the current line in the console), so we use the left-shift operator (<) to separate them. Endl stands for End Line, and it does exactly that.
At this point, you’re probably wondering why we have semicolons. Semicolons tell the compiler where statements end. This whole program could be on a single line, however we use whitespace (space which contains no code) to make the code easier to read. To have all the code be on a single line, we would simply need to write it all on the same line, separating our commands / statements using semicolons. You put semicolons at the end of your commands / statements, signifying that another command / statement is coming after that semicolon.
Next, we will examine cin.get();. This command tells the console to wait for user input.
Now we can finally run our code. Simply press “F5” on your keyboard (or select “start debugging” from the visual studio debug menu) to run the code. You will probably be met with a message about your code being “out of date”. This is a normal Visual Studio warning, so simply click “yes” to continue running your program. If you get any errors, here is the full code:
// Hello World!.cpp : Defines the entry point for the console application. // #include <iostream> int main() { std::cout << "Hello World!" << std::endl; std::cin.get(); return 0; }
Cheers :)!
Questions
What does #include <filename> do?
Answer: replace the #include statement with the code located in the filename
What is the preprocessor?
Answer: The preprocessor runs through your code before compiling and readies it for compiling
What does cin.get() do?
Answer: wait for user input
What does cout do?
Answer: Prints to the console
What does the insertion operator do?
Answer: Inserts data into cout
What does endl do?
Answer: End the current line in the console
Coming Next Lesson:
- Variables!
- Main Function!
- Console Input / Output!
Article Change-Log!
- Removed
using namespace std;
command! - Changed _tmain() to main() and explained main()!
- Fixed last source code!
- Clarified Left-Shift Operator!
- Fixed Source Code!
- Clarified the purpose of SemiColons!
- Fixed more Source Code!
- Removed #include "stdafx.h"!