Headline

Implementation in Language:CPlusPlus, a medium-level, multi-paradigm language with a rich standard library.

Characteristics

While C++ supports multiple programming paradigms, the 101 Companies problem is still best modelled using OO programming. However, with its powerful templates (or generics), the language favors general algorithms separate from the types they can operate on.

Despite that, this contribution's objects are far from behaviorless structs, primarily because they have constructors and destructors. Since destructors are deterministically and recursively called when an object goes out of scope, the language supports Resource Acquisition Is Initialization. Hence, and opposed to Contribution:c, memory is never manually allocated or deallocated.

Since the language supports constant values, there is also a focus on so-called Const-correctness. Constant values, including objects, are recursively constant (not just super shallow final pointers like in Language:Java). This makes it easier for both humans and the compiler to reason about the program.

Illustration

Data Model

The object model is relatively straightforward for a OO programming language. Each of the classes has a default constructor (one that can be invoked without any arguments) so that its objects can be used in containers. It also has getters, setters and visitor acceptors inherited from the "Host" virtual base class Note that methods that do not modify the object are marked as "const" and the ones that do are not.

class Company : public Host {
public:
    Company(const std::string            & name        = "",
            const std::vector<Department>& departments = {});
    // Getters are const
    const std::string            & getName       () const;
    const std::vector<Department>& getDepartments() const;
    // Setters are non-const
    void setName       (const std::string            & name       );
    void setDepartments(const std::vector<Department>& departments);
    // Special getter to retrieve a mutable reference
    std::vector<Department>& getMutableDepartments();
    // Constant acceptor
    void accept(const Visitor      & visitor) const override;
    // Mutable acceptor
    void accept(const MutateVisitor& visitor)       override;
    // private members omitted
};
// anologous for Department and Employee

Graph Traversal

Most features require traversal of the object graph, either accumulating information about the objects or manipulating them. This traversal is solved via registering Closures in a Visitor.

An example for an accumulating feature is Feature:Total. Note that all given parameters are marked "const":

double
total(const Host& h)
{
    double total = 0;
    h.accept(Visitor()
        .onEnter([&](const Employee& e){
            total += e.getSalary();
        }));
    return total;
}

An example for a manipulating feature is Feature:Cut, where the parameters are not "const" and an appropriate MutateVisitor is used:

void
cut(Host& h)
{
    h.accept(MutateVisitor()
        .onEnter([](Employee& e){
            const double& sal = e.getSalary();
            e.setSalary(sal ? sal / 2 : 0);
        }));
}

Relationships

Architecture

As is usual for C++, interface and implementation are separated into header files (declarations) and implementation files (definitions). This is mostly due to the language's primitive import capabilities: when a file is included, its contents just get dumped into the current compilation unit, only after compilation are these separate parts linked together.

Header files (.hpp) are found in the include directory and implementation files (.cpp) are found in the src directory.

Usage

This contribution uses Technology:CMake for handling its build process and requires the Technology:Boost libraries, specifically Boost.PropertyTree and Boost.Test. Please see the readme.md on GitHub for details.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.