Overview

C++ is an object-oriented language created in the 1980’s by Bjarne Stroustrup, developed on top of the C language and inspired by Simula.

It has been designed with efficiency and performance in mind, so is still widely used in applications in which speed and memory usage are critical, like desktop applications, system libraries, embedded systems, or real-time server applications.

C++ has a very rich and powerful syntax, which makes it a very difficult language to master. Programming in C++ requires a strong discipline: as in C, the program has direct access to memory and operating system primitives, so any mistake easily leads to crashes, or worse, undefined behavior.

Due to its complex syntax, especially when using templates, writing large maintainable C++ applications is a real challenge, that’s why simpler languages like Java or C# are now more popular in the field of desktop and server applications.

Hello, World!

To see what the language looks like, nothing is better than the traditional « Hello World » program, so here it is:

#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Does the syntax look too simple? Then let’s see a more real-life example (written in C++14), split in 3 different source files:

// hello.hpp

#include <ostream>

class HelloPrinter {
public:
    virtual ~HelloPrinter() = default;
    virtual void sayHello(const std::string& world) = 0;
};

class HelloStreamPrinter: public HelloPrinter {
public:
    HelloStreamPrinter(std::ostream& writer): _writer(writer) {}
    void sayHello(const std::string& world) override;

private:
    std::ostream& _writer;
};
// hello.cpp

#include "hello.hpp"

using namespace std;

void HelloStreamPrinter::sayHello(const string& world)
{
    _writer << "Hello, " << world << "!" << endl;
}
// main.cpp

#include <iostream>
#include <memory>
#include "hello.hpp"

using namespace std;

int main(int argc, const char **argv)
{
    unique_ptr<HelloPrinter> printer(new HelloStreamPrinter(cout));
    printer->sayHello("World"s);
    return 0;
}

Now this is real C++! This code does exactly the same thing (print « Hello, World! »), but covers many C++ features:

  • Pre-processor directives
  • Compilation units
  • Functions
  • Classes and objects
  • Constructors and destructors
  • Inheritance
  • Polymorphism and virtual methods
  • Memory management
  • Pointers, references and smart pointers
  • Templates
  • STL (Standard Template Library)
  • Inline functions
  • Literal strings
  • Operator overloading
  • Namespaces

Fully understanding all these concepts already requires a significant learning effort! And this is quite basic C++ code actually 😉

This is not really material for beginners, but you will probably have to refer to these links if you write C++ for real: