Programming in C++

Introduction

Gerald Senarclens de Grancy

Advantages of C++

Multi-Paradigm

Proper implementation of multiple programming paradigms

  • Procedural programming
  • Object-Oriented programming (OOP)
  • Generic programming
  • Functional programming

High-Level Programming

The standard library provides algorithms and containers allowing the programmer to focus on the problem.

Third party libraries cover almost everything well (except the web).

Examples include boost, SDL, wxWidgets and many more. See this comprehensive list of open source C++ libraries.

  • Low-level programming close to the hardware is possible (like in C)
  • High performance (fast execution speed)
  • Exception handling, namespaces, ...
  • Large community (TIOBE Index, Open Hub Language Comparison)
    • Open source compilers and tools
    • Abundance of resources for learning
    • Ever-evolving

Brief History

1979
Bjarne Stroustrup started development of C++ at Bell Labs
1985
first edition of C++ was released
1989
C++ 2.0
1998
C++98 was released, standardizing the language
2011
C++11 offers numerous new features and enlarges the standard library
thereafter
C++14, C++17 and C++20
C++23 in the making

Differences from C

C++ is not a strict superset of C.

Code that is valid C is not automatically valid C++.

For example, C++ offers many additional features compared to C. These include proper object orientation, anonymous functions, operator overloading, templates and many more.

Therefore, C++ uses many additional keywords that cannot be used as identifiers anymore. These include auto, class, namespace, new ...

Example

#include <stdio.h>

int main() {
  int new = 3;
  printf("%d\n", new);
  return 0;
}

Download valid_c.c

Wikipedia has more information on compatibility of C and C++

Structure of a C++ Program

#include <iostream>

/* Hello World program written in C++. */
int main(void) {
  std::cout << "Hello, World!" << std::endl;  // output some text
  return 0;  // 0 means success
}

Visualize execution

Preprocessor Directives

Allow to include code declared elsewhere

#include <iostream>

/* Hello World program written in C++. */
int main(void) {
  std::cout << "Hello, World!" << std::endl;  // output some text
  return 0;  // 0 means success
}

Comments

// single line comment
int x = 5;  // comments can be added at the end of a line

/*
 * multiline comment
 * ...
 */

Removed by the preprocessor

Main Function

Serves a special purpose in C++ programs:
program execution begins with main(.)

#include <iostream>

/* Hello World program written in C++. */
int main(void) {
  std::cout << "Hello, World!" << std::endl;  // output some text
  return 0;  // 0 means success
}

Return Value

Indicate success (0) or a failure code to the calling operating system shell

#include <iostream>

/* Hello World program written in C++. */
int main(void) {
  std::cout << "Hello, World!" << std::endl;  // output some text
  return 0;  // 0 means success
}

Compiling and Executing a C++ Program

Executable program files are created from readable source code.

[cpp source icon]
main.cpp
[cpp source icon]
executable

For creating executables, a compiler toolchain is needed.

  • GCC - GNU Compiler Collection
  • Clang - C language family frontend for LLVM
  • MSVC - Microsoft Visual C and C++ compiler

Creating a C++ executable is a four step process.

  1. Preprocessor
  2. Compiler
  3. Assembler
  4. Linker

Example

Download hello.cpp

By default, all four stages (preprocessor, compiler, assembler and linker)
are done

clang++ hello.cpp  # or ` hello.cpp`
./a.out

Provide a name for the created executable (instead of the default a.out

clang++ hello.cpp -o hello
./hello

Compilation can be done with either clang++ or .

clang++ $INFILE -o $PROGRAM_NAME
 $INFILE -o $PROGRAM_NAME

1. Preprocessor

  • Essentially a pure text processor
  • Removes comments, performs includes etc.
  • Usually never done separately

Stop after preprocessor and send output to stdout

clang++ -E hello.cpp

2. Compiler

  • Translates high level code to a lower level (assembly language)
  • Parses the source code
  • Performs type checking
  • Usually never done separately
  • Must recognize all identifiers (function declarations etc.)

Creates assembler files (usually *.s)

clang++ -S hello.cpp

3. Assembler

  • Translates assembly language to object code (machine language)
  • Creates *.o files
  • Usually done separately when working with multiple input files
    • Impossible to create executables without main(.)
    • Impossible to create executables with multiple main(.)
clang++ -c hello.cpp

4. Linker

  • Combines object files into a unified executable program, resolving all symbols
  • Should be run separately when working with multiple input files
clang++ -o hello hello.o

Questions
and feedback...