Skip to the content.

C++

Information

C++ is a compiled, statically typed, general-purpose programming language that extends C with object-oriented programming, templates (generic programming), and the Standard Template Library (STL). It is used for performance-critical applications: embedded systems, game engines, operating systems, high-frequency trading, scientific computing, and graphics.

Key characteristics:

Installation

Rocky Linux / CentOS / Fedora

sudo dnf install -y gcc-c++ clang cmake make
g++ --version
clang++ --version
cmake --version

Debian / Ubuntu

sudo apt install -y g++ clang cmake make

Build Tools

# CMake-based project
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

Package Managers

Coding standard

Include loading order

Include as:

  1. corresponding header file to current .cpp file (with “”)
  2. current project header files (with “”)
  3. third party header files (with <>)
  4. standard C header files (with <>)
  5. standard C++ header files (with <>)
  6. system header files (with <>)

Rules

Example code blocks

A.h

#ifndef SET_MY_INFO_A_H
#define SET_MY_INFO_A_H

namespace set_my_info {
    class A {
    public:
        A();
        virtual ~A();
        void DoSomething();
    private:
        int data_length_;// trailing _ for class members
    };
}

#endif // SET_MY_INFO_A_H

A.cpp

#include "A.h"

using set_my_info::A;

A::A(): data_length_(0) {
    // Const impl
}

A::~A() {
    // Dest impl
}

void A::DoSomething() {
    // Impl
}

C functions in C++.

#ifdef __cplusplus
extern "C" {
#endif

void MyCFunction(int arg1, int arg2);

#ifdef __cplusplus
}
#endif

Compare with Java

#ifndef SET_MY_INFO_A_H
#define SET_MY_INFO_A_H

namespace SetMyInfo {
    class A {
    public:
        void DoSomething();

        // Overridable. Non-pure function.
        virualt void DoSomethingElse();

        // Pure virtual function like in java abstract. Makes class like Java abstract class.
        virualt void DoSomethingElse2() = 0;
    };

    class B : A {
    public:

        // Like @Override in Java
        void DoSomethingElse() = override {};

        // Like @Override in Java
        void DoSomethingElse2() = override {};
    };
}

#endif // SET_MY_INFO_A_H

Function composition

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums {1, 2, 3, 4, 5};

  // Lambda funktsiooni creation
  auto double_num = [](int num) { return num * 2; };

  // Apply lambda funktsion to all elements in a vector
  std::transform(nums.begin(), nums.end(), nums.begin(), double_num);

  // Output vector
  for (auto num : nums) {
    std::cout << num << " ";
  }

  return 0;
}

Returning

  1. Return class instances by value (return by value) and caller should use ‘const’ keyword for function return value to avoid class copy. Compiler optimizes and uses caller stack instance.
Example GetExampleReturnByValue() {
    Example example(123);
    // do work
    return example;
}

void CallerFunction() {
    const Example example = GetExampleReturnByValue();
    // do work
}
  1. Or return pointer and put it immediately into smart pointer, to be released when exiting scope. Use this form at low level services/components.
Example *GetExampleReturnByPointer() {
    Example *example = new Example(123);
    // do work
    return example;
}

void FiveTwo() {
    // Or std::shared_ptr
    std::unique_ptr<Example> example_smart_pointer(GetExampleReturnByPointer());
    // do work
}

See also