A C++ program has a very specific structure in terms of how the code is written and some key elements that you use in your C++ programs.

Here, we have a program called hello.cpp. It is a classic first program!

// This program outputs the message "Hello World!" to the monitor

#include <iostream>

using namespace std;

int main()
{

    cout << "Hello World!\n";
    return 0;

}

This program writes the phrase "Hello, World!" to your terminal.

C++ is a case sensitive language. Case sensitivity means that your keywords and variable declarations must match the case. For example, the C++ keyword for outputting is cout. If you were to type Cout or COUT, the compiler would not know that your intention was to use the keyword cout.

Note: There are a lot of new concepts introduced in this article. Don't be intimidated! You'll learn more about these things in the next few weeks (and a whole lot more).


Let's go over this hello.cpp program line by line:

  • // This program outputs the message "Hello World!" to the monitor
    
    This is a single-line comment that documents this code. The compiler will ignore everything after // to the end of the line.
  • #include <iostream>
    
    This is known as a pre-processor directive. It instructs the compiler to locate the file that contains code for a library known as iostream. This library contains code that allows for input and output, such as displaying data in the terminal window, or reading input from your keyboard.
  • using namespace std;
    
    A namespace contains resources we can quickly reference in our code. The std namespace (short for "standard") contains lots of helpful resources!
  • int main() { 
    }
    
    Every C++ program must have a function called main(). A function is basically a sequence of instructions for the computer to execute. This main() function houses all of our instructions for our program. This is where we will be writing our code.
  • cout << "Hello World!\n";
    
    This code uses a method known as cout (pronounced "see out") to send the text "Hello World!" to the terminal for output and display.
  • return 0;
    
    return is a special keyword that is used at the end of a function. In this case, we're using the value of 0 to indicate that the program finished successfully.

C++ source code also permits judicious use of white space (tabs, spaces, new lines) to create code that is easier to read. The compiler completely ignores the white space, with a small exception concerning if statements that will be covered later. It is highly recommended that you make use of white space to indent and separate lines of code to aid in readability of your source code files.


The Process

C++ is a compiled language. That means that to get a program to run, you must first translate it from the human-readable form to something a machine can "understand." That translation is done by a program called a compiler.

What you read and write is called source code (usually it's in a English-like language like C++), and what the computer executes is called executable, object code, or machine code (a machine language).

Typically C++ source code files are given the suffix:

  • .cpp (ex: hello.cpp) or
  • .h (ex: std_lib_facilities.h).

And object code files are given the suffix .obj (on Windows) or .o (on Unix).

title

As a C++ programmer, you mainly follow these steps during development:

  1. Code — writing the program
  2. Save — saving the program
  3. Compile — via the terminal
  4. Execute — via the terminal

And repeat (debug the errors if needed):

Programming Flow

Compile:

g++ hello.cpp

A compiler translates the C++ program into machine language code which it stores on the disk as a file with the extension .o ( e.g. hello.o). A linker then links the object code with standard library routines that the program may use and creates an executable image which is also saved on disk, usually as a file with the file name without any extension (e.g. hello).

Execute:

./a.out

The executable is loaded from the disk to memory and the computer's CPU (Central Processing Unit) executes the program one instruction at a time.

Video walkthrough coming soon!

Made in NYC © 2018 Codecademy