Why should I learn C++

Why should I learn C++

What language is useful to learn? Is it C++? In this article history, strengths and weaknesses will be discussed.

According to stackoverflow's developer survey 2018, C++ is still a viable choice. Thus, learning C++ is still a consideration to start off learning programming.

General

Bjarne Stroustrup is the inventor of C++. He developed and created this programming language. As he is one of the old guys, the language exists for more than 30 years.

Inspired by C and driven by Object Oriented Programming, short OOP, paradigm C++ is one of the few languages you are not forced to program object oriented. It allows Functional Programming and a lot of other paradigms, too.

What is Object Oriented Programming (OOP)?

Object oriented programming is a way of designing your programs. Using OOP, you have to think in objects. Those are real life objects. For instance, you have an object called calculator. It has several functions like adding two numbers together, displaying some numbers on it's display, etc. Using OOP you map this way of thinking to programming.

What is Functional Programming?

Unlike object oriented programming, following functional programming paradigm you stick to operations. It's like a cooking recipe: you have different ingredients (data) and different cooking steps (functions). Following your recipe and sticking to it's plan, you are practically running a program. You start at the beginning, some steps are repeated and at the end you will receive your result.

Use Cases

When do people use C++ in their programs?

  • low level controller software for kitchen machines, cars and alike (embedded programming)
  • used for operating systems running on your computer or mobile device (low level programming)
  • programming games and entertainment software (game engineering)
  • memory, graphic and computing intense applications (graphic programming, artificial intelligence)

Strengths

  • widely used, high influence on new language inventions and thus helps to learn programming languages like Java, C#, etc.
  • many information sources available
  • you will understand how computers internally work (pointers, memory)
  • theoretically C++ allows to write code running on any machine (portability)
  • possibility to integrate assembly code
  • allows high performance resulting in fast executing programs

Weaknesses

  • complexity is very high and steep learning curve especially at the beginning
  • supports nearly all available coding styles (clean code is important)
  • data mistakes will not be forgiven, e.g. adding 15 to letter c is not possible (type-safety)
  • compiler is merciless and error messages are sometimes confusing
  • many additional libraries available for different application areas (but may be an advantage, too)

Example Code

How does C++ code look like? Here are two examples...

Hello World

#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
    return 0;
}

Visitor Pattern

Source: https://en.wikipedia.org/wiki/Visitor_pattern

#include <iostream>
#include <vector>

class AbstractDispatcher;   // Forward declare AbstractDispatcher

class File {    // Parent class for the elements (ArchivedFile, SplitFile and ExtractedFile)
public:
    // This function accepts an object of any class derived from AbstractDispatcher and must be implemented in all derived classes
    virtual void accept(AbstractDispatcher &dispatcher) = 0;
};

// Forward declare specific elements (files) to be dispatched
class ArchivedFile;
class SplitFile;
class ExtractedFile;

class AbstractDispatcher {   // Declares the interface for the dispatcher
public:
    // Declare overloads for each kind of a file to dispatch
    virtual void dispatch(ArchivedFile &file) = 0;
    virtual void dispatch(SplitFile &file) = 0;
    virtual void dispatch(ExtractedFile &file) = 0;
};

class ArchivedFile: public File {    // Specific element class #1
public:
    // Resolved at runtime, it calls the dispatcher's overloaded function, corresponding to ArchivedFile.
    void accept(AbstractDispatcher &dispatcher) override {
        dispatcher.dispatch(*this);
    }
};

class SplitFile: public File {   // Specific element class #2
public:
    // Resolved at runtime, it calls the dispatcher's overloaded function, corresponding to SplitFile.
    void accept(AbstractDispatcher &dispatcher) override {
        dispatcher.dispatch(*this);
    }
};

class ExtractedFile: public File {   // Specific element class #3
public:
    // Resolved at runtime, it calls the dispatcher's overloaded function, corresponding to ExtractedFile.
    void accept(AbstractDispatcher &dispatcher) override {
        dispatcher.dispatch(*this);
    }
};

class Dispatcher: public AbstractDispatcher {   // Implements dispatching of all kind of elements (files)
public:
    void dispatch(ArchivedFile &) override {
        std::cout << "dispatching ArchivedFile" << std::endl;
    }

    void dispatch(SplitFile &) override {
        std::cout << "dispatching SplitFile" << std::endl;
    }

    void dispatch(ExtractedFile &) override {
        std::cout << "dispatching ExtractedFile" << std::endl;
    }
};

int main() {
    ArchivedFile archivedFile;
    SplitFile splitFile;
    ExtractedFile extractedFile;

    std::vector<file*> files;
    files.push_back(&archivedFile);
    files.push_back(&splitFile);
    files.push_back(&extractedFile);

    Dispatcher dispatcher;

    for (File* file : files) {
        file->accept(dispatcher);
    }
}

Conclusion

To put it in a nutshell, C++ is a viable choice for learning a high-level programming language. It exists for many years already and helps to understand mechanics and internas of many other computer languages.

Further reading