CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine helper functions into a single class.

Putting it together

In the final lesson we bring the three helpers together: capitalize, clamp and formatDate. The previous lessons' solution is now ready inside the Helpers class. We will call all three in main and print their results.

This lesson's starting code contains all the methods from the previous lesson. All you need to do is add three calls inside main. This shows how the library is used. Output: Hello, 10, 2024-03-15 — each on a separate line.

Using the helpers together

We call the three helpers one after another in main. We separate each call with std::endl to show the output on separate lines. Helpers::capitalize("hello") returns a string, Helpers::clamp(15, 0, 10) a number, Helpers::formatDate(2024, 3, 15) a string again.

Despite three different return types, std::cout prints all of them. This is the flexibility of C++'s stream architecture. To extend the library, you just add a new static method to the Helpers class; you do not need to change existing code.

Practice Call all three helpers

In main call all three helpers: std::cout << Helpers::capitalize("hello") << std::endl;, std::cout << Helpers::clamp(15, 0, 10) << std::endl; and std::cout << Helpers::formatDate(2024, 3, 15);. Each output on a separate line.

Starter code

main.cpp
#include <iostream>
#include <string>
#include <cctype>

class Helpers {
public:
    static std::string capitalize(std::string text) {
        if (text.empty()) return text;
        text[0] = std::toupper(text[0]);
        return text;
    }
    static int clamp(int value, int min, int max) {
        if (value < min) return min;
        if (value > max) return max;
        return value;
    }
    static std::string formatDate(int year, int month, int day) {
        std::string m = std::to_string(month);
        if (m.length() == 1) m = "0" + m;
        std::string d = std::to_string(day);
        if (d.length() == 1) d = "0" + d;
        return std::to_string(year) + "-" + m + "-" + d;
    }
};

int main() {
    // Üç yardımcıyı da çağır ve sonuçlarını yazdır
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>
#include <cctype>

class Helpers {
public:
    static std::string capitalize(std::string text) {
        if (text.empty()) return text;
        text[0] = std::toupper(text[0]);
        return text;
    }
    static int clamp(int value, int min, int max) {
        if (value < min) return min;
        if (value > max) return max;
        return value;
    }
    static std::string formatDate(int year, int month, int day) {
        std::string m = std::to_string(month);
        if (m.length() == 1) m = "0" + m;
        std::string d = std::to_string(day);
        if (d.length() == 1) d = "0" + d;
        return std::to_string(year) + "-" + m + "-" + d;
    }
};

int main() {
    std::cout << Helpers::capitalize("hello") << std::endl;
    std::cout << Helpers::clamp(15, 0, 10) << std::endl;
    std::cout << Helpers::formatDate(2024, 3, 15);
    return 0;
}

Expected output

Hello
10
2024-03-15

Library complete

Congratulations! You completed the 5-lesson helper library project. The Helpers class now has three useful static methods: capitalize, clamp and formatDate. Each does a specific job and is called with the Helpers:: prefix.

Throughout this project you practiced class design, static methods, member functions and a cumulative code structure. In the next section we will deepen encapsulation: private members, const methods, mutable and friend to protect data more tightly.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why is a helper library gathered in one class?

main.cpp
class Helpers { public: static ... };
  • To avoid name clashes and ease use Correct answer
  • To save memory
  • To run faster
  • The compiler requires it

Explanation: One class acts like a namespace and makes use easier.

2 Complete the capitalize call

Question
std::cout << ____::capitalize("hello");
Answer
std::cout << Helpers::capitalize("hello");

Explanation: Helpers::capitalize is a static method call.

All lessons in Intermediate