Can anyone tell me how exactly exercise work in Exercism

I’m intermediate in c++ and can easily solve leetcode and other CPP questions, but I don’t understand what kind of problem statement is and how we solve it. Like it’s kind of weird can anyone please explain to me how to solve questions here? Like how to handle this .h and .cpp header files and all in solution

One of the fundamental ideas of Exercism is to help programmers who already know a programming language learn other programming languages. That’s why (almost) all practice exercises start with a language-agnostic description and some test data.

That gets translated to the various programming languages and allows the “students” to transfer their knowledge, solve the same exercise in multiple programming languages, see what’ similar, what’s different, what’s easy, what’s hard, how idiomatic code looks in a specific language, etc.

So the concrete requirements are in the tests.


This is the file hello_world_test.cpp from the hello-world exercise

// Include the header file with the definitions of the functions you create.
#include "hello_world.h"

// Include the test framework.
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif

// Declares a single test.
TEST_CASE("test_hello")
{
    // Check if your function returns "Hello, World!".
    REQUIRE(hello_world::hello() == "Hello, World!");
}

It first includes hello_world.h which is part of your solution. Then the tests call your functions and compare the actual result with the expected result.
In this case hello_world() gets called and is required to return the string "Hello, World".

Your implementation (the .cpp file), the tests, and the testing framework (Catch2) get compiled into an program and executed. If you’ve solved the exercise all tests pass.


What does that mean for you?

In the .h file you have to declare the functions (and in later exercises define data structures) that the tests can use.
In the .cpp file you have to define those functions (and maybe additional implementation details like helper functions).

That’s exactly how most “real” C++ projects are organized. The .h files contain the “interface”, the .cpp files contain the implementation.

Let’s look at the the initial (failing) solution of the hello-world exercise.
This is the file hello_world.h:

// This is an include guard.
// You could alternatively use '#pragma once'
// See https://en.wikipedia.org/wiki/Include_guard
#if !defined(HELLO_WORLD_H)
#define HELLO_WORLD_H

// Include the string header so that we have access to 'std::string'
#include <string>

// Declare a namespace for the function(s) we are exporting.
// https://en.cppreference.com/w/cpp/language/namespace
namespace hello_world {

// Declare the 'hello()' function, which takes no arguments and returns a
// 'std::string'. The function itself is defined in the hello_world.cpp source
// file. Because it is inside of the 'hello_world' namespace, it's full name is
// 'hello_world::hello()'.
std::string hello();

}  // namespace hello_world

#endif

As you can see it already declares the function hello() in the namespace hello_world. Nothing to be done here for you.

This is the file hello_world.cpp:

#include "hello_world.h"

using namespace std;

namespace hello_world
{

string hello()
{
    return "Goodbye, Mars!";
}

}

It already defines the function hello(), but that function currently returns the wrong string.

In this first exercise all you have to do is correct the string.


But don’t worry, not all exercises are that dull ;-)

In the next few exercises the .h file already contains the declarations of the functions. You have to define them now in the .cpp file.

The later exercises want you to do even more. There the initial .h file is almost empty. You have to read the tests to figure out which functions and data type are required, declare them in the .h file and define them in the .cpp file.
That was an intentional decision because there are multiple slightly different ways to specify the return and parameter types, that is all part of your solution.

Did that help?

4 Likes