Initial Course

This commit is contained in:
David Gil de Gómez Pérez
2026-08-11 17:40:36 +03:00
commit 9aed1d1d86
202 changed files with 3420 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
add_course_exercise(NAME function_basics MODULE 03_functions STANDARD 17
SOURCES src/function_basics.cpp test/function_basics_test.cpp)
add_course_exercise(NAME overloading MODULE 03_functions STANDARD 17
SOURCES src/overloading.cpp test/overloading_test.cpp)
add_course_exercise(NAME default_parameters MODULE 03_functions STANDARD 17
SOURCES src/default_parameters.cpp test/default_parameters_test.cpp)
add_course_exercise(NAME recursion MODULE 03_functions STANDARD 17
SOURCES src/recursion.cpp test/recursion_test.cpp)
+17
View File
@@ -0,0 +1,17 @@
# Module 03: Functions
## Learning Goals
- Declare and define functions with clear interfaces
- Pass arguments by value, reference, and `const` reference
- Overload functions and use default parameters
- Implement recursive algorithms safely
## Exercises
| Exercise | Topic |
|----------|-------|
| `function_basics` | Value/reference parameters, return values |
| `overloading` | Function overloading |
| `default_parameters` | Default arguments |
| `recursion` | Recursive algorithms |
@@ -0,0 +1,11 @@
#pragma once
#include <string>
namespace functions {
[[nodiscard]] auto repeat(char ch, int count = 1) -> std::string;
[[nodiscard]] auto power(int base, int exponent = 2) -> long long;
[[nodiscard]] auto clamp(int value, int min = 0, int max = 100) -> int;
} // namespace functions
@@ -0,0 +1,13 @@
#pragma once
#include <string>
#include <vector>
namespace functions {
[[nodiscard]] auto square(int value) -> int;
void swap_integers(int& lhs, int& rhs);
[[nodiscard]] auto concatenate(const std::vector<std::string>& parts) -> std::string;
void append_suffix(std::string& text, const std::string& suffix);
} // namespace functions
@@ -0,0 +1,13 @@
#pragma once
#include <string>
namespace functions {
[[nodiscard]] auto max_value(int a, int b) -> int;
[[nodiscard]] auto max_value(double a, double b) -> double;
[[nodiscard]] auto max_value(const std::string& a, const std::string& b) -> std::string;
[[nodiscard]] auto area(int side) -> int;
[[nodiscard]] auto area(int width, int height) -> int;
} // namespace functions
@@ -0,0 +1,9 @@
#pragma once
namespace functions {
[[nodiscard]] auto fibonacci(int n) -> long long;
[[nodiscard]] auto sum_digits(int n) -> int;
[[nodiscard]] auto is_palindrome(int n) -> bool;
} // namespace functions
@@ -0,0 +1,9 @@
#include "default_parameters.hpp"
namespace functions {
auto repeat(char ch, int count) -> std::string { (void)ch; (void)count; return {}; }
auto power(int base, int exponent) -> long long { (void)base; (void)exponent; return 0; }
auto clamp(int value, int min, int max) -> int { (void)value; (void)min; (void)max; return 0; }
} // namespace functions
@@ -0,0 +1,10 @@
#include "function_basics.hpp"
namespace functions {
auto square(int value) -> int { (void)value; return 0; }
void swap_integers(int& lhs, int& rhs) { (void)lhs; (void)rhs; }
auto concatenate(const std::vector<std::string>& parts) -> std::string { (void)parts; return {}; }
void append_suffix(std::string& text, const std::string& suffix) { (void)text; (void)suffix; }
} // namespace functions
+11
View File
@@ -0,0 +1,11 @@
#include "overloading.hpp"
namespace functions {
auto max_value(int a, int b) -> int { (void)a; (void)b; return 0; }
auto max_value(double a, double b) -> double { (void)a; (void)b; return 0.0; }
auto max_value(const std::string& a, const std::string& b) -> std::string { (void)a; (void)b; return {}; }
auto area(int side) -> int { (void)side; return 0; }
auto area(int width, int height) -> int { (void)width; (void)height; return 0; }
} // namespace functions
+9
View File
@@ -0,0 +1,9 @@
#include "recursion.hpp"
namespace functions {
auto fibonacci(int n) -> long long { (void)n; return 0; }
auto sum_digits(int n) -> int { (void)n; return 0; }
auto is_palindrome(int n) -> bool { (void)n; return false; }
} // namespace functions
@@ -0,0 +1,18 @@
#include "default_parameters.hpp"
#include <gtest/gtest.h>
TEST(DefaultParameters, Repeat) {
EXPECT_EQ(functions::repeat('*'), "*");
EXPECT_EQ(functions::repeat('-', 3), "---");
}
TEST(DefaultParameters, Power) {
EXPECT_EQ(functions::power(5), 25);
EXPECT_EQ(functions::power(2, 10), 1024);
}
TEST(DefaultParameters, Clamp) {
EXPECT_EQ(functions::clamp(150), 100);
EXPECT_EQ(functions::clamp(-5), 0);
EXPECT_EQ(functions::clamp(42, 10, 50), 42);
}
@@ -0,0 +1,21 @@
#include "function_basics.hpp"
#include <gtest/gtest.h>
TEST(FunctionBasics, Square) { EXPECT_EQ(functions::square(7), 49); }
TEST(FunctionBasics, SwapIntegers) {
int a = 1, b = 2;
functions::swap_integers(a, b);
EXPECT_EQ(a, 2);
EXPECT_EQ(b, 1);
}
TEST(FunctionBasics, Concatenate) {
EXPECT_EQ(functions::concatenate({"C", "++"}), "C++");
}
TEST(FunctionBasics, AppendSuffix) {
std::string text = "modern";
functions::append_suffix(text, "_cpp");
EXPECT_EQ(text, "modern_cpp");
}
@@ -0,0 +1,10 @@
#include "overloading.hpp"
#include <gtest/gtest.h>
TEST(Overloading, MaxInt) { EXPECT_EQ(functions::max_value(3, 9), 9); }
TEST(Overloading, MaxDouble) { EXPECT_DOUBLE_EQ(functions::max_value(3.1, 2.9), 3.1); }
TEST(Overloading, MaxString) { EXPECT_EQ(functions::max_value("abc", "abd"), "abd"); }
TEST(Overloading, Area) {
EXPECT_EQ(functions::area(4), 16);
EXPECT_EQ(functions::area(3, 5), 15);
}
@@ -0,0 +1,17 @@
#include "recursion.hpp"
#include <gtest/gtest.h>
TEST(Recursion, Fibonacci) {
EXPECT_EQ(functions::fibonacci(0), 0);
EXPECT_EQ(functions::fibonacci(1), 1);
EXPECT_EQ(functions::fibonacci(10), 55);
}
TEST(Recursion, SumDigits) {
EXPECT_EQ(functions::sum_digits(12345), 15);
}
TEST(Recursion, IsPalindrome) {
EXPECT_TRUE(functions::is_palindrome(121));
EXPECT_FALSE(functions::is_palindrome(123));
}