Lecture 1: Introduction to C++

At its very core, the personal computer (PC) is just a central processor unit (CPU) with some memory (RAM), capable of executing a tiny set of instructions like, “store 255 in memory location 0xff6900h.” However, with an operating system (OS), a high-level programming language, an integrated development environment (IDE), compiler, and some programming knowledge we’re capable of doing much more with a PC.

 

What is C++ ?

C++ is a multi-paradigm, statically typed, case sensitive, high-level programming language. The C++ birth dates back to year 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. C++ inherits the C language, however, supports object-oriented programming (OOP). Stroustrup began working on a project he called “C with classes”, which as the name implies it supports OOP without losing the portability, speed or low-level functionality that C provides. C++ today is immensely popular across the world, and it is mainly used for its speed and/or the low-level features it provides.
 

The Compilation Process

A program goes from C++ source files into processor instructions as follows:

Compilers and assemblers emit object files containing the generated binary code and data for a source file. The Linker then links together multiple object files into one; and the loader takes the produced object files and loads them into memory.
 
An object file is the real output from the compilation phase that represents an incomplete copy of the program. One single C++ source file expresses the whole program, while in the case of multiple C++ source files, each C++ source file express a piece of the program, once compiled into an object file, the outputted object file has some markers indicating what pieces it depends on. The Linker takes all the emitted object files the compiled libraries of predefined code that they rely on, and links them together into the final program, which can then be run by the OS.
 
The Compiler and the Linker are just two simple regular programs. The first step in the compilation phase is known as parsing—which the compiler reads the source code file(s).
 
The main reason C++ code runs far faster than code in many more recent languages, is because all these steps are performed ahead of time, before you start running a program. In some programming languages, these steps are performed during the execution process, which takes time.
 
In addition, C++ actually adds an extra step to the compilation process, the code runs through a preprocessor. The preprocessor is a program that is invoked by the compiler to process code before compilation. Thus, the modified illustration becomes:

 

Hello World

It is an honer to start this paragraph with the traditional “Hello,world!” program as an entry point into the basic features of C++.

// A Hello World Program
#include <iostream>

using namespace std; 

int main()
{
    cout << "Hello World" << endl; 
    cin.get(); 
    return 0; 
}

 

Line-By-Line Explanation

  1. The very first line at the top of our Hello World program is a simple comment.
    // A Hello World Program

    Comments are ignored by the compiler, and are used to explain the non-obvious things going on in the code. Comments exist in two forms:

    // Single-line comment
    /* Multiple-lines comment */

    In conclusion, comments should be used to document your code and explain different parts of the code.
     

  2. On the next line we find our very first pre-processing directive, #include. All pre-processing directives start with a hash sign (“#”). Pre-processing directives are used to change what code is actually being compiled.
     #include <iostream>

    #include tells the preprocessor to treat the contents of a specified file, in this context the ‘iostream’ file, as if those contents had appeared in the source program.

  3.  

  4. Below line 2, comes an interesting piece of code, a namespace is used.
    using namespace std;

    The namespace keyword is used to organize code and provides a unique way to share identifiers. The using namespace [something]; is used to avoid the constant usage of the scope resolution operator (::)—for example, in the Hello World code example, the using namespace std; tells the compiler that it should look in the std namespace for any identifier that we have not defined. If we do this, we can omit the std:: prefix when writing cout, this is the recommended practice in general when writing C++ code.

  5.  

  6. Finally, our very first program function, int main() { . . . }—defines the entry-point that is executed when the program starts up. Everything between the curly brackets ({ }) represent multiple statements in a code block. I’ll be covering more about the syntax in the next few lectures.
  7.  

  8. There are three statements inside the main() function:
    cout << "Hello World" << endl;
    cin.get();
    return 0;

    Statement 1: cout is used to output a string (text) to the console. Std:cout is an object of the ostream class that represents the standard output stream. insertion operator (<<) is used to pass in a string or any other variable that is then outputted to the console on the screen. The endl; ends the line by inserting a new-line character (‘\n’ a.k.a. escape sequence).
     
    Statement 2: The Std::cin is the opposite of the cout, cin is an object of the istream class that represents the standard input stream. However, cin is used differently in this context, I am accessing a function, the get() function in the cin object. The cin.get() forces the program to wait for the user to enter a key before it can close, and you can see the output of your program.
     
    Statement 3: The return 0; statement indicates that the program has completed successfully.

 
 
That’s a lot of information, huh? Do not worry, I’ll be explaining more in coming lectures about C++. Till next time, enjoy this lecture and have fun!
 
Cheers,
Fisnik