|
||
|
|
| Learn to program in C++ | ||||||
| Posted July 2, 2006. Written by John Zervos, author of the text Byte Into C++ (2002). Tertiary Press: Melbourne. | ||||||
| This
article provides a comprehensive yet simple to understand treatment of the
C++ language for beginners. The article is divided into a series of
separate lessons as follows:
Lesson 1 - Introduction |
||||||
|
|
1: #include <iostream> 2: using std::cout; 3: 4: int main() 5: { 6: cout << "Hello World"; 7: return 0; 8: } |
This program may seem a little convoluted but it contains a fair bit of overhead that you'll need to include in every program. The words shown in bold text are keywords that are recognised by the compiler and cannot be used for any other purpose. Let's look at each line in more detail. Please remember that the line numbers are shown for convenience only and do not form part of the program. They should not be included when writing the program.
Line 1: #include is a preprocessor directive, meaning that it runs prior to compilation. This directive tells the compiler to include additional source code in your program from the file iostream. This file contains information needed by the program to perform simple input-output tasks, and should be included in every console (text-based) application you write. The file is supplied as part of your C++ compiler software.
Line 2: This line tells the compiler to only use the cout object from iostream, which is used in line 6 to display a message on the screen. This statement, like all complete C++ statements, must be terminated with a semicolon.
Line 3: This is a blank line to separate the first two lines from the start of the main function. It is ignored by the compiler.
Line 4: This is the start of the main function. A function is a stand-alone block of code and every program must contain a main function as this tells the compiler where to begin program execution. The name of the function is main and, like all function names, is followed by a pair of parentheses (round brackets). The int tells the compiler that the function will return an integer (whole number) value (to the operating system or calling program) on completion. All functions are delimited by a pair of opening and closing braces (curly brackets) as seen in lines 5 and 8. All of the code for a function must lie within these braces.
Line 5: This is the opening brace for the main function.
Line 6: This is the main statement that does all the work. The cout object is used with the "put to" or "insertion" operator, <<, to send data to the standard output device (screen). The data being sent in this case is the string (text) "Hello World". The string must be surrounded by double quotation marks. The operation of the cout object is very complex. For now you can think of it as a tool for displaying information on the screen. The statement is terminated with a semicolon.
Line 7: The return keyword ends execution of the function and returns the integer value 0 back to the program that executed your program (normally the operating system). This is customary and the "0" denotes that the program ended normally and without error. This statement it is also terminated with a semicolon.
Line 8: This is the closing brace for the main function. It will terminate execution of the function if a return statement does not precede it.
A limitation with this program is that it does not display a new line character after printing "Hello World". This can be done by embedding the control character \n at the end of the string like this "Hello World\n" or by cascading the cout statement using an additional << and the endl manipulator which is defined in iostream. The improved program looks like this:
|
1: #include <iostream> 2: using std::cout; 3: using std::endl; 4: 5: int main() 6: { 7: cout << "Hello World" << endl; 8: return 0; 9: } |
The changes can be seen in lines 3 and 7 in the program directly above. Line 3 tells the compiler to use the endl operator from iostream. In line 7, you can see where the << operator has been cascaded and the endl inserted.
The procedure for running and testing your program will depend on the C++ development tools you are using. if you are using a GUI IDE, you'll need to start a new C++ source file or console application, type in the code, compile, build, and run it.
Depending on the compiler you are using, you may find that the executable program ends abruptly without giving you the opportunity of seeing the results of your hard work. If this is the case, there are several ways to get around it. One way, while not recommended for commercial programs, is to send a system command that causes the console to pause. In DOS and Windows based systems, you can insert the following line before the return statement:
system("pause");
This statement sends the "pause" command to the command line interface.
Depending on the compiler you are using, you may find that the executable program ends abruptly without giving you the opportunity of seeing the results of your hard work. If this is the case, there are several ways to get around it. One way, while not recommended for commercial programs, is to send a system command that causes the console to pause. In DOS and Windows based systems, you can insert the following line before the return statement:
The term whitespace refers to blank lines, tabs, and spaces. These are ignored by the compiler and so should be used generously to make the code easy to read. You can see from the code examples above that the statements within a function, that is, between the braces, are indented using the Tab key. This aids clarity and is an accepted programming style.
C++ is a case-sensitive language. This means that the compiler differentiates between uppercase and lowercase letters. As a general rule, most of the code is written in lowercase. Uppercase letters can be used in strings and comments. User-defined names (identifiers), used later in later lessons, can also used uppercase letters but they must subsequently be used in the same case in which they were first declared (created).
Like all programming languages, C++ allows you to add comments in your code to aid readability and understanding. Comments are ignored by the compiler and so do not affect the final executable code. There are two ways to use comments in your programs.
The older style C method is to block out one or more lines of comments using /* and */. In C++ you can precede any line, or part of a line, with // which will comment out the remainder of that line only. Both types of comments are shown below:
|
/* My first program. July 2006. helloworld.cpp */ #include <iostream> using std::cout; using std::endl; int main() { cout << "Hello World" << endl; //Displays a message. return 0; } |
Congratulations! You're now well on the way to learning how to program in C++. Be sure to check out the next lesson.
Lesson 2 - Data types and variables
| © 2006 learn2dostuff.com |