Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Alternatively, for a first-time C++ learner, there are several other resources:
- [The C++ FAQ](https://isocpp.org/wiki/faq)
- [C++ Core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)

*cplings got its inspiration from the magnificient [rustlings repository](https://github.com/rust-lang/rustlings), which aims at familiarizing newcomers to the Rust language. This repository is a sort of a fork of it but for the C++ language*
*cplings got its inspiration from the magnificent [rustlings repository](https://github.com/rust-lang/rustlings), which aims at familiarizing newcomers to the Rust language. This repository is a sort of a fork of it but for the C++ language*


## Getting Started
Expand All @@ -23,7 +23,7 @@ Install and Usage Demo video:
[![Install and Usage Demo](https://img.youtube.com/vi/18vNfxwU5n4/0.jpg)](https://youtu.be/18vNfxwU5n4)

## Windows
Visit https://visualstudio.microsoft.com/vs/community/ and download Visual Studio Community.
Visit https://visualstudio.microsoft.com/vs/community/ and download Visual Studio Community.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the change here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see, there is this red bar at the end which displays a trailing space... Same for the others.

When, installing it make sure to select Desktop development with C++ and then, in the "Installation details" menu enable support for the C++ CMake Tools for Windows

## Linux
Expand All @@ -45,18 +45,18 @@ cmake ..
make
```

You should expect the build to fail : your task is to fix each exercise for the build to succed.
You should expect the build to fail. Your task is to fix each exercise for the build to succeed.

## Windows
Install Visual Studio Community Edition and Git.
Install Visual Studio Community Edition or Code and Git.

```cmd
```sh
# Clone this repository
git clone https://github.com/rdjondo/cplings

```

Open the folder cplings in Visual Studio. Add support for CMake and run a build. You should expect the build to fail : your task is to fix each exercise for the build to succed.
Open the folder cplings in Visual Studio. Add support for CMake and run a build. You should expect the build to fail. Your task is to fix each exercise for the build to succeed.


## Doing the exercises
Expand All @@ -76,12 +76,12 @@ This will try to verify the completion of every exercise in a predetermined orde

OR, to run a specific exercise, tell make with exercise to run. For example:

```bash
```sh
make variable2
```

### In Windows
Visual Studio will let you choose the specific exercise you would like to solve.
Visual Studio will let you choose the specific exercise you would like to solve.
Comment thread
jaques-sam-tlv marked this conversation as resolved.

## Hints
You will find some help for specific exercises in the hints directory. Please note that this is work in progress.
Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// even after you already figured it out.

int type_function(int input) {
x = input;
Comment thread
jaques-sam-tlv marked this conversation as resolved.
x = input;
return x;
}

Expand Down
1 change: 0 additions & 1 deletion exercises/01_variables/variables3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// even after you already figured it out.

int double_value_function(const int input) {
// Solution fix the next line variable : note input cannot be modified
return input;
}

Expand Down
3 changes: 1 addition & 2 deletions exercises/01_variables/variables4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

// This example showcases the use of a constexpr (computable at compile time)
constexpr int double_var(const int x){
// Solution fix the next line variable : note input cannot be modified
Comment thread
jaques-sam-tlv marked this conversation as resolved.
return x;
}

int double_value_function(int x) {
return double_var(x);
return double_var(x);
Comment thread
jaques-sam-tlv marked this conversation as resolved.
}


Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

int output_10_function() {
const int y = ;
return y;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What has changed?

return y;
}


Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const int y = ; // y is a global variable. Fix it's definition.
int global_var_10_function() {
return y;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What has changed?

return y;
}


Expand Down
4 changes: 2 additions & 2 deletions exercises/01_variables/variables7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// This exercises showcases integer overflows. This occurs when the
// selected datatype is not large enough to hold the value.
// See https://www.learncpp.com/cpp-tutorial/fixed-width-integers-and-size-t/
// See: https://www.learncpp.com/cpp-tutorial/unsigned-integers-and-why-to-avoid-them/
// See https://www.learncpp.com/cpp-tutorial/unsigned-integers-and-why-to-avoid-them/

const int y = 10;
const int y = 10;
long long integer_char_sizes() {
const char a = 10;
const char b = ; // Fix: set expected value to pass the test
Expand Down
6 changes: 3 additions & 3 deletions exercises/01_variables/variables8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void init_words(){
const std::string secret_key = "Encrypted S3cr3t!";

std::string test_danger_loop(uint32_t query_idx) { // Use safe<> type
// Tip: if you cannot find the problem replace all uint32_t with
// Tip: if you cannot find the problem replace all uint32_t with

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What has changed?

// safe<uint32_t>, from the boost::safe_numerics library
// The safe<uint32_t> will throw an exception to help you find the bug

Expand All @@ -59,7 +59,7 @@ TEST_CASE("integer_signedness") {
std::cout << "Next index to query " << i << ", this is word: " << words[i] << "\n";
REQUIRE(test_danger_loop(i) == expected_words.at(i));
}
std::cout << "Next index to query is dangerous :" << 4 << ", this is word:" << words[4] << "\n";

std::cout << "Next index to query is dangerous: " << 4 << ", this is word: " << words[4] << "\n";
REQUIRE(test_danger_loop(len + 1) == "");
}
2 changes: 1 addition & 1 deletion exercises/02_functions/functions3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// even after you already figured it out.


void callme(int x) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference?

void callme(int x) {
for (int i = 0; i < x; i++) {
std::cout << "Ring! Call number " << i + 1 << "\n";
}
Expand Down
4 changes: 2 additions & 2 deletions exercises/02_functions/functions5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ constexpr void is_even(int num) {
auto sale_price = []() -> int {
if (is_even(price))
return price - 10;
else
else
return price - 3;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the change?

};

int function_syntax() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_functions/functions6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Make me compile! Go to the folder hint if you want a hint :)

// We sometimes encourage you to keep trying things on a given exercise,
// even after you already figured it out.
// even after you already figured it out.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the change?


// Step 1: Make me compile and pass!

Expand Down
2 changes: 1 addition & 1 deletion exercises/05_classes/classes2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Point {
}
};

constexpr Point point_zero = ;
constexpr Point point_zero = ;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the change?


class Shape {
public:
Expand Down
2 changes: 1 addition & 1 deletion exercises/05_classes/classes4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Shape {

class Circle : public Shape {
public:
Circle(const Point& centre = point_zero) : Shape(centre) { }
Circle(const Point& centre = point_zero) : Shape(centre) { }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the change?

virtual Point center() const {
return Circle::center_;
}
Expand Down
12 changes: 6 additions & 6 deletions exercises/06_raii/raii1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Make me compile! Go to the folder hint if you want a hint :)

// We sometimes encourage you to keep trying things on shape given exercise,
// even after you already figured it out.
// even after you already figured it out.
Comment thread
jaques-sam-tlv marked this conversation as resolved.

// Step 1: Make me compile and pass the test!

Expand All @@ -20,11 +20,11 @@ class ExpensiveResource {
};
ExpensiveResource(std::string n = "") : name(n) {
resources_count += 1;
std::cout << "Opening Ressource " << name << ", new count is:"<< resources_count << "\n";
std::cout << "Opening Resource " << name << ", new count is:"<< resources_count << "\n";
}
~ExpensiveResource() {
resources_count -= 1;
std::cout << "Closing Ressource " << name << ", new count is:" << resources_count << "\n";
std::cout << "Closing Resource " << name << ", new count is:" << resources_count << "\n";
}
};
int ExpensiveResource::resources_count = 0;
Expand All @@ -34,7 +34,7 @@ class Holder {
ExpensiveResource * resource;

public:
Holder(std::string n = "") {
Holder(std::string n = "") {
Comment thread
jaques-sam-tlv marked this conversation as resolved.
resource = new ExpensiveResource(n);
}
// Fix: I need a destructor that releases the resource
Expand All @@ -53,7 +53,7 @@ void test_raii1() {

TEST_CASE("test_raii1") {
test_raii1();
std::cout << "To avoid leaks the final number of ressources should be 0 \n";
std::cout << "The actual number of ressources " << ExpensiveResource::getResourceCount() << "\n";
std::cout << "To avoid leaks the final number of resources should be 0 \n";
std::cout << "The actual number of resources " << ExpensiveResource::getResourceCount() << "\n";
REQUIRE(ExpensiveResource::getResourceCount() == 0);
}
20 changes: 10 additions & 10 deletions exercises/06_raii/raii2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include <string>
#include <unordered_set>

// raii1.cpp
// raii2.cpp
// Make me compile! Go to the folder hint if you want a hint :)

// We sometimes encourage you to keep trying things on shape given exercise,
// even after you already figured it out.
// even after you already figured it out.

Comment thread
jaques-sam-tlv marked this conversation as resolved.
// Step 1: Make me compile and pass the test! Here a set is used to avoid creating more resources when the same name is used

Expand All @@ -21,11 +21,11 @@ class ExpensiveResource {
};
ExpensiveResource(std::string n = "") : name(n) {
resources. ...; // Fix: perform an operation on resources
std::cout << "Opening Ressource " << n << ", new count is:"<< resources.size() << "\n";
std::cout << "Opening Resource " << n << ", new count is:"<< resources.size() << "\n";
}
~ExpensiveResource() {
resources. ...; // Fix: perform an operation on resources
std::cout << "Closing Ressource " << name << ", new count is:" << resources.size() << "\n";
std::cout << "Closing Resource " << name << ", new count is:" << resources.size() << "\n";
}
};
std::unordered_set<std::string> ExpensiveResource::resources;
Expand All @@ -35,7 +35,7 @@ class Holder {
ExpensiveResource * resource;

public:
Holder(std::string n = "") {
Holder(std::string n = "") {
resource = new ExpensiveResource(n);
}
~Holder() { // TODO: Delete the destructor
Expand All @@ -44,7 +44,7 @@ class Holder {
};


void test_raii1() {
void test_raii2() {
Holder hold1{ "First hold" };
Holder hold2{ "First hold" }; // calling to hold same resource, the count should not change
Holder hold3{ "Third hold" };
Expand All @@ -53,9 +53,9 @@ void test_raii1() {

#include <catch2/catch.hpp>

TEST_CASE("test_raii1") {
test_raii1();
std::cout << "To avoid leaks the final number of ressources should be 0 \n";
std::cout << "The actual number of ressources " << ExpensiveResource::getResourceCount() << "\n";
TEST_CASE("test_raii2") {
test_raii2();
std::cout << "To avoid leaks the final number of resources should be 0 \n";
std::cout << "The actual number of resources " << ExpensiveResource::getResourceCount() << "\n";
REQUIRE(ExpensiveResource::getResourceCount() == 0);
}
10 changes: 5 additions & 5 deletions exercises/07_containers/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Standard Containers
Here, you'll learn how to write a to reconize when standard containers may be useful.
Here, you'll learn how to recognize when standard containers may be useful.


## Book Sections
Introduction to # Standard Containers -
(Introduction to Vectors)[https://www.cplusplus.com/reference/vector/]
(Introduction to Unordered Sets)[https://www.cplusplus.com/reference/unordered_set/unordered_set/] and (Sets)[https://www.cplusplus.com/reference/set/set/] )
(Introduction to Unordered Map)[https://www.cplusplus.com/reference/unordered_map/unordered_map/] and (Map)[https://www.cplusplus.com/reference/map/map/] )
Introduction to Standard Containers -
[Introduction to Vectors](https://www.cplusplus.com/reference/vector/)
[Introduction to Unordered Sets](https://www.cplusplus.com/reference/unordered_set/unordered_set/) and [Sets](https://www.cplusplus.com/reference/set/set/)
[Introduction to Unordered Map](https://www.cplusplus.com/reference/unordered_map/unordered_map/) and [Map](https://www.cplusplus.com/reference/map/map/)
4 changes: 2 additions & 2 deletions exercises/07_containers/containers1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Make me compile! Go to the folder hint if you want a hint :)

// We sometimes encourage you to keep trying things on shape given exercise,
// even after you already figured it out.
// even after you already figured it out.
Comment thread
jaques-sam-tlv marked this conversation as resolved.

// Step 1: Make me compile. Replace the array with the appropriate container.

Expand All @@ -26,7 +26,7 @@ void test_containers1() {
TEST_CASE("test_containers1") {
test_containers1();
std::cout << "Size of container : " << my_sequence.size() << "\n";
REQUIRE(my_sequence.size() == num_elements);
REQUIRE(my_sequence.size() == num_elements);

for (const auto& e : my_sequence) {
std::cout << "Value :" << e << "\n";
Expand Down
2 changes: 1 addition & 1 deletion exercises/07_containers/containers3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Make me compile! Go to the folder hint if you want a hint :)

// We sometimes encourage you to keep trying things on shape given exercise,
// even after you already figured it out.
// even after you already figured it out.

// Step 1: Make me compile. Replace the array with the appropriate container.

Expand Down
14 changes: 7 additions & 7 deletions exercises/08_ownership/ownership3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class ExpensiveResource {
}
ExpensiveResource(std::string n = "") : name(n) {
resources.insert(this);
std::cout << "Opening Ressource " << n << ", new count is:" << resources.size() << "\n";
std::cout << "Opening Resource " << n << ", new count is:" << resources.size() << "\n";
}
~ExpensiveResource() {
resources.erase(this);
std::cout << "Closing Ressource " << name << ", new count is:" << resources.size() << "\n";
std::cout << "Closing Resource " << name << ", new count is:" << resources.size() << "\n";
}
};
std::unordered_set<ExpensiveResource*> ExpensiveResource::resources;
Expand All @@ -52,7 +52,7 @@ class Holder {
}
};

using HolderBox = std::unique_ptr<Holder>; // Note that HolderBox is a unique_ptr to Holder
using HolderBox = std::unique_ptr<Holder>; // Note that HolderBox is a unique_ptr to Holder

void push_data(std::vector<HolderBox>& holder_list, HolderBox hold_ptr) {
holder_list.push_back( ...? hold_ptr);
Expand All @@ -78,8 +78,8 @@ std::vector<HolderBox> test_ownership3() {
TEST_CASE("test_ownership3_0") {
std::vector<HolderBox> v = test_ownership3();
std::cout << "\ntest_ownership1\n";
std::cout << "The actual number of ressources " << ExpensiveResource::getResourceCount() << "\n";
std::cout << "The actual number of ressources owned " << v.size() << "\n";
std::cout << "The actual number of resources " << ExpensiveResource::getResourceCount() << "\n";
std::cout << "The actual number of resources owned " << v.size() << "\n";

REQUIRE(ExpensiveResource::getResourceCount() == 3);
REQUIRE(ExpensiveResource::getResourceCount() == v.size());
Expand All @@ -90,7 +90,7 @@ TEST_CASE("test_ownership3_0") {

TEST_CASE("test_ownership3_1") {
std::cout << "\ntest_ownership2\n";
std::cout << "To avoid leaks the final number of ressources should be 0 \n";
std::cout << "The actual number of ressources " << ExpensiveResource::getResourceCount() << "\n";
std::cout << "To avoid leaks the final number of resources should be 0 \n";
std::cout << "The actual number of resources " << ExpensiveResource::getResourceCount() << "\n";
REQUIRE(ExpensiveResource::getResourceCount() == 0);
}
Loading