Lesson 1: Introduction

Read this post to understand what the objectives of this course is and also know what preparations are needed for this course. After you installed the IDE, you can proceed with this lesson.

A basic C++ program

Type the following code into your IDE. Ignore all the line labels e.g. 1:, 2:.

1:// Program 1.1
2:#include <iostream>
3:int main (int argc, const char * argv[]) {
4:  std::cout << "Hello World!" << std::endl;
5:  return 0;
6:}

Since this is your first program, we will spend a little time explaining all the statements.

Statement 1 starts with //. Two backslashes denote a comment. Anything after the two backslashes are ignored by the compiler till the end of the statement. End of statement is usually represented by “Enter” key strike.

Statement 2 is called the preprocessor. A computer does not know anything unless the programmer tell it what to do. Although the compiler contains some very basic functions that allow you to create variables (lesson 2), the more complicated functions such as printing to the console need to be defined in details. These details are located in the library iostream. io means input/output, and iostream handles all operations that involves input and output. In program 1.1, you want to output the statement “Hello World!” to the console. Hence, you need to call the iostream library using the preprocessor #include.

Statement 3 is the declaration of the main function. A C++ program always starts with the main function. Your program must have this statement. int argc, const char * argv[] is enclosed in the pair of parenthesis. This statement forms the arguments for the main function. Arguments are the inputs to a function. You will learn more about arguments in later lessons on functions. The argument is needed if you want to add inputs when you run the program in command line.

In statement 4, std::cout refers to the standard output. Generally, this means the console, which the the screen. Here, you will print the sentence “Hello World!” onto the screen. std::endl will cause the cursor to move to the next statement. It is a good practice to use it at the end of a std::cout statement. All statements must end with a semicolon ;.

Some functions return values. main function is one such function. Since main function was declared as int main(), it will return a value. Returning 0 means the program runs successfully.

The body of a function is enclosed in a pair of braces {}. Hence, you need to end the main function with the closing } in statement 6.

Compiling and running program

A computer only understands binary numbers 1 and 0. It is not going to understand the code that you typed in Program 1.1. In order to translate this code to machine language, you need to compile it.

In most IDE, compiling a program requires you to select the Build function from the menu bar. Locate Build in your IDE and click on it. After that, select and click Run. Some IDE compiles and run if you click Run while others run only the last compiled program. If you make changes and click Run, you might not get the result from the latest changes. Hence, it is a good practice to click Build follow by Run.

img001

After running, you will get the output Hello World! on the screen. Notice that the cursor is on the next line.

Challenge 1

Write a C++ program that greets you. The output should be similar to

Good Day Stanley!

print