Skip to content

Commit a807217

Browse files
chore: backup
1 parent 8d39455 commit a807217

121 files changed

Lines changed: 1949 additions & 309 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

exercises/00_intro/01_intro.cpp

100755100644
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1+
// TODO: We sometimes encourage you to keep trying things on a given exercise
2+
// even after you already figured it out. If you got everything working and feel
3+
// ready for the next exercise, enter `n` in the terminal.
4+
5+
#include <gtest/gtest.h>
16
#include <iostream>
7+
#include <string>
28

3-
void splashScreen()
9+
void intro01()
410
{
11+
// TODO: Enter the correct passphrase.
12+
// NOTE: Use the testcase as reference.
13+
std::cout << "Welcome to " << "cpplings" << "!" << std::endl;
14+
}
515

6-
// // user input
7-
// char userInput{};
16+
int main(int argc, char *argv[])
17+
{
18+
intro01();
819

9-
// splash screen
10-
// std::cout << " Welcome to cpplings! \n";
11-
std::cout << " _ _ \n";
12-
std::cout << " | (_) \n";
13-
std::cout << " ___ _ __ _ __ | |_ _ __ __ _ ___ \n";
14-
std::cout << " / __| '_ \\| '_ \\| | | '_ \\ / _` / __|\n";
15-
std::cout << "| (__| |_) | |_) | | | | | | (_| \\__ \\\n";
16-
std::cout << " \\___| .__/| .__/|_|_|_| |_|\\__, |___/\n";
17-
std::cout << " | | | | __/ | \n";
18-
std::cout << " |_| |_| |___/ \n";
19-
std::cout << " \n";
20-
// std::cout << " Enter Y/y to continue \n";
20+
std::cout << "\n\n" << "Testing output begins here\n--------------------------" << "\n";
21+
testing::InitGoogleTest(&argc, argv);
22+
return RUN_ALL_TESTS();
23+
}
2124

22-
// while (true)
23-
// {
24-
// std::cin >> userInput;
25-
//
26-
// if (userInput == 'y' || userInput == 'Y')
27-
// {
28-
// break;
29-
// }
30-
// }
25+
TEST(Intro, Intro01)
26+
{
27+
testing::internal::CaptureStdout();
28+
intro01();
29+
std::string stdout{testing::internal::GetCapturedStdout()};
30+
ASSERT_EQ(stdout, "Welcome to cpplings!\n");
3131
}

exercises/00_intro/02_intro.cpp

100755100644
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22
// even after you already figured it out.
33
//
44
// Printing in C++ involves "cout", a function from the "std" namespace,
5-
// that displays an stream buffer in the console.
5+
// that displays a stream buffer in the console.
66
//
77
// Referencing a function from a namespace, or in this a case a namespace
8-
// from a module (imported using "#include <iostream>"),
8+
// from a module/header (imported using "#include <iostream>"),
99
// involves using the scope resolution operator (::),
10-
// we will get deeper into modules later.
10+
// we will get deeper into modules and headers later.
1111
//
1212
// The value to be printed must "shift to the stream" using the
1313
// left-shift operator (<<).
1414
//
1515
// https://cppbyexample.com/what_is_iostream.html
1616
// http://www.en.cppreference.com/w/cpp/io/cout.html
17+
// https://www.learncpp.com/cpp-tutorial/statements-and-the-structure-of-a-program/
1718

19+
#include <gtest/gtest.h>
1820
#include <iostream>
1921

20-
void message()
22+
void intro02()
2123
{
22-
// TODO: Fix the line of code to print "Hello World!" to the console
23-
std::cout << "Hello World!";
24+
// TODO: Fix the line of code to print "Hello World!" to the console.
25+
std::cout << "Hello ";
26+
}
27+
28+
int main()
29+
{
30+
intro02();
2431
}

exercises/01_variables/01_variables.cpp

100755100644
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,36 @@
66
// datatype variableName = value;
77
//
88
// Using the first example is called Direct List Initialization.
9-
// It is a modern way of intializing variables in C++
9+
// It is a modern way of intializing variables in C++.
1010
//
1111
// The second method is a classic way of initializing and assigning
1212
// values to variables.
1313
//
14+
// In C++, variables can be named whatever using different conventions unlike
15+
// certain languages like Rust or Python where they must be named in snake_case.
16+
// In C/C++, variables are often named using the camelCase or snake_case convention.
17+
//
1418
// https://www.learncpp.com/cpp-tutorial/introduction-to-objects-and-variables/
1519
// https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/
1620

21+
#include <gtest/gtest.h>
22+
1723
// TODO: Add the missing module for printing
1824
#include <iostream>
1925

2026
void variables01()
2127
{
22-
// TODO: Add the missing keyword
28+
// TODO: Assign an integer to a variable named "x" using direct list initialization.
2329
int x{5};
2430

25-
std::cout << x;
31+
// TODO: Assign an integer to a variable named "y" using the classic way.
32+
int y = 10;
33+
34+
std::cout << "direct list initialization -> " << "x{" << x << "}" << "\n";
35+
std::cout << "classic way -> " << "y" << "=" << y;
36+
}
37+
38+
int main()
39+
{
40+
variables01();
2641
}

exercises/01_variables/02_variables.cpp

100755100644
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
// A variable's type must be declared before
2+
// the variable's name.
3+
//
4+
// int foo{5};
5+
// float bar{10.5f}; / float bar{10.5};
6+
// double baz{20.56};
7+
18
#include <iostream>
29

310
void variables02()
411
{
5-
// TODO: Add the missing keyword
12+
// TODO: Add a missing type and value.
613
int x{5};
714

8-
std::cout << x;
15+
std::cout << "x=" << x;
16+
}
17+
18+
int main()
19+
{
20+
variables02();
921
}

exercises/01_variables/03_variables.cpp

100755100644
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Besides the variable data types, C++ has a few more
2+
// keywords for declaring the type of variable before
3+
// decalaring the variable's data type.
4+
//
5+
// const FOO{5};
6+
//
7+
// Constant names are normally written in UPPER_CASE or CONSTANT_CASE.
8+
//
9+
// https://www.learncpp.com/cpp-tutorial/keywords-and-naming-identifiers/
10+
11+
#include <iostream>
12+
#include <type_traits>
13+
14+
void variables03()
15+
{
16+
// TODO: Declare a constant variable with a value and then print it out to stdout.
17+
const int FOO{5};
18+
19+
std::cout << FOO;
20+
21+
static_assert(std::is_const_v<decltype(FOO)>,
22+
"The variable must be a constant."); // NOTE: Ignore this line of code.
23+
}
24+
25+
int main()
26+
{
27+
variables03();
28+
}

exercises/01_variables/04_variables.cpp

100755100644
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
int x;
1+
#include <gtest/gtest.h>
2+
#include <string>
23

34
void variables04()
45
{
6+
// TODO: Change the line below to fix the compiler error.
7+
// NOTE: Use the test case as reference.
8+
int baz{5};
9+
10+
// TODO: Also print out the value of baz to stdout in a sentence "baz=5".
11+
std::cout << "baz=" << baz;
12+
}
13+
14+
int main(int argc, char **argv)
15+
{
16+
variables04();
17+
18+
std::cout << "\n\n" << "Testing output begins here\n--------------------------" << "\n";
19+
testing::InitGoogleTest(&argc, argv);
20+
return RUN_ALL_TESTS();
21+
}
22+
23+
TEST(Variables, Variables04)
24+
{
25+
testing::internal::CaptureStdout();
26+
variables04();
27+
std::string stdout{testing::internal::GetCapturedStdout()};
28+
ASSERT_EQ(stdout, "baz=5");
529
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TODO: We sometimes encourage you to keep trying things on a given exercise
2+
// even after you already figured it out.
3+
//
4+
// C++ is a statically typed language.
5+
// Datatypes need to be explicitly decalred.
6+
// Datatypes can be "inferred" by using the "auto" keyword.
7+
8+
#include <iostream>
9+
int main()
10+
{
11+
// TODO: Infer the datatype using the "auto" keyword.
12+
// Assign a "truthy" value to compile.
13+
auto doesAutoWork{true};
14+
15+
std::cout << "doesAutoWork? -> " << doesAutoWork;
16+
17+
if (!doesAutoWork)
18+
{
19+
return 1;
20+
}
21+
}

exercises/02_functions/01_functions.cpp

100755100644
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,58 @@
1+
// TODO: We sometimes encourage you to keep trying things on a given exercise
2+
// even after you already figured it out.
13
//
4+
// Evey programmer knows that functions are resuable pieces of code.
25
//
6+
// Each function is called and assigned/"pushed" on to the stack (fragment of memory).
7+
// Read more about the stack here: https://en.wikipedia.org/wiki/Stack-based_memory_allocation/
8+
//
9+
// A C++ function includes:
10+
// - The function signature: int f(int x)
11+
// | | |
12+
// | | parameter list (parameter datatype parameter name, ...)
13+
// | |
14+
// | function name
15+
// |
16+
// function return type
17+
//
18+
// - Function clause/body:
19+
// { -> function scope beginning
20+
// return x; -> return (depends on function return type; will be explained in an upcoming exercise)
21+
// } -> function scope ending
22+
//
23+
// Functions are normally named using the camelCase convention.
324
//
425
// https://www.learncpp.com/cpp-tutorial/introduction-to-functions/
526
// https://www.learncpp.com/cpp-tutorial/function-return-values-value-returning-functions/
27+
// https://www.learncpp.com/cpp-tutorial/why-functions-are-useful-and-how-to-use-them-effectively/
628

729
#include <gtest/gtest.h>
830
#include <iostream>
931

10-
int function01(int x)
32+
// TODO: Create the function signature.
33+
// Add the function return type, name "f" and a parameter "x" with an int datatype.
34+
int f(int x)
1135
{
1236
int y{x + 1};
1337

38+
std::cout << "x: " << x << " " << "y: " << y << std::endl;
39+
1440
return y;
1541
}
1642

43+
int main(int argc, char **argv)
44+
{
45+
// TODO: Call the function and pass in an appropriate parameter.
46+
f(10);
47+
48+
std::cout << "\n\n" << "Testing output begins here\n--------------------------" << "\n";
49+
testing::InitGoogleTest(&argc, argv);
50+
return RUN_ALL_TESTS();
51+
}
52+
1753
TEST(Functions, Function01)
1854
{
19-
ASSERT_EQ(function01(0), 1);
20-
ASSERT_EQ(function01(1), 2);
21-
ASSERT_EQ(function01(2), 3);
55+
ASSERT_EQ(f(0), 1);
56+
ASSERT_EQ(f(1), 2);
57+
ASSERT_EQ(f(2), 3);
2258
};

exercises/02_functions/02_functions.cpp

100755100644
Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
1-
// https://www.learncpp.com/cpp-tutorial/void-functions-non-value-returning-functions/
1+
// The function return a value and the value's datatype must be the same
2+
// as the function's return type.
23
//
4+
// https://www.learncpp.com/cpp-tutorial/function-return-values-value-returning-functions/
5+
// https://www.learncpp.com/cpp-tutorial/introduction-to-function-parameters-and-arguments/
36

4-
void function02()
7+
#include <gtest/gtest.h>
8+
#include <string>
9+
10+
// TODO: Add the missing keyword.
11+
// NOTE: Use the rest of the code as reference to determine which datatype needs to be returned.
12+
int bar()
13+
{
14+
// TODO: Change the line below to fix the compiler error.
15+
// NOTE: Use the test case as reference.
16+
int baz{5};
17+
18+
// TODO: Also print out the value of baz to stdout in a sentence "baz=5".
19+
std::cout << "baz=" << baz;
20+
21+
return baz;
22+
}
23+
24+
int main(int argc, char **argv)
25+
{
26+
// TODO: call the bar function
27+
bar();
28+
29+
std::cout << "\n\n" << "Testing output begins here\n--------------------------" << "\n";
30+
testing::InitGoogleTest(&argc, argv);
31+
return RUN_ALL_TESTS();
32+
}
33+
34+
TEST(Variables, Variables04)
535
{
36+
testing::internal::CaptureStdout();
37+
bar();
38+
std::string stdout{testing::internal::GetCapturedStdout()};
39+
ASSERT_EQ(bar(), 5);
40+
ASSERT_EQ(stdout, "baz=5");
641
}

0 commit comments

Comments
 (0)