Skip to content

Commit 0248b6f

Browse files
committed
Added the solution to Lesson 2
1 parent ce9e5f6 commit 0248b6f

File tree

20 files changed

+236
-0
lines changed

20 files changed

+236
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

Lesson 2/Activity 4/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# BeginningCPlusPlus
2+
3+
Compile the activity by running `compile.bat`.

Lesson 2/Activity 4/compile.bat

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build log.cpp, but do not link
5+
:: /c means compile only, do not link
6+
:: /Fo specifies the folder where to put the output
7+
cl /EHsc /c /Fobuild/ log.cpp
8+
9+
:: Build main.cpp, but do not link
10+
:: /I adds a folder to the folders where to look for includes.
11+
:: We add the current folder because we are including log.h
12+
cl /EHsc /c /Fobuild/ /I . main.cpp
13+
14+
:: Link the two object files together
15+
LINK build\log.obj build\main.obj /OUT:build\main.exe
16+
17+
:: You can not run the program at build\main.exe

Lesson 2/Activity 4/log.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The header iostream contains std::cout and std::endl
2+
#include <iostream>
3+
4+
// Define the log function. We already declared it in the .h file
5+
void log() {
6+
std::cout << "Error!" << std::endl;
7+
}

Lesson 2/Activity 4/log.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Declare the log function, which takes no arguments and returns nothing
2+
void log();

Lesson 2/Activity 4/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The loh.h header declares the log function
2+
#include <log.h>
3+
4+
// main is the entry point of the program: it is called when the program starts
5+
int main() {
6+
log(); // call the function log which is defined in the loh.h header
7+
}

Lesson 2/Activity 5/compile.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build and link main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 2/Activity 5/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
3+
// global variables accessible by the whole file.
4+
// Since we don't want to modify them, we mark them with const.
5+
const int POSITION = 10;
6+
const int ALREADY_COMPUTED = 3;
7+
8+
// Define and declare the function
9+
void print_tenth_fibonacci() {
10+
// Local variables.
11+
// They don't have to be defined at the top of the function!
12+
int n_1 = 1;
13+
int n_2 = 0;
14+
int current = n_1 + n_2;
15+
// We can access the global variables from inside the function
16+
for(int i = ALREADY_COMPUTED; i <= POSITION; ++i) {
17+
current = n_1 + n_2;
18+
n_2 = n_1;
19+
n_1 = current;
20+
}
21+
std::cout << current << std::endl;
22+
}
23+
24+
int main() {
25+
print_tenth_fibonacci();
26+
}

Lesson 2/Activity 6/compile.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build and link main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 2/Activity 6/main.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// std::array<Type, Number of Elements> is defined in the array header
2+
#include <array>
3+
#include <iostream>
4+
5+
// The size of the arguments is small: take by value
6+
int sum(int a, int b)
7+
{
8+
return a + b;
9+
}
10+
11+
// We need to modify the result, so we need to use non-const references for the arrays
12+
int& getMaxOf(std::array<int, 10>& array1, std::array<int, 10>& array2, int index) {
13+
if (array1[index] >= array2[index]) {
14+
return array1[index];
15+
} else {
16+
return array2[index];
17+
}
18+
}
19+
20+
int main() {
21+
// We pass by value, so we can use temporary values safely
22+
std::cout << "sum(2, 3) = " << sum(2, 3) << std::endl;
23+
24+
std::array<int, 10> sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
25+
std::array<int, 10> reversed = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
26+
std::cout << "sorted[0] = " << sorted[0] << "\n" << "reversed[0] = " << reversed[0] << std::endl;
27+
28+
// We want to modify the element, so we need to make sure we take the result as reference.
29+
// If we were to return the result as a value, when we modify it it will not propagate
30+
// to the original element
31+
int& element = getMaxOf(sorted, reversed, 0);
32+
std::cout << "Max: " << element << std::endl;
33+
element = 0;
34+
std::cout << "sorted[0] = " << sorted[0] << "\n" << "reversed[0] = " << reversed[0] << std::endl;
35+
std::cout << "The original value has been updated though the reference!" << std::endl;
36+
37+
// Note: we are taking by value. This creates a new local copy, and changes to it will not modify
38+
// the original value
39+
int value = getMaxOf(sorted, reversed, 0);
40+
value = 10;
41+
std::cout << "sorted[0] = " << sorted[0] << "\n" << "reversed[0] = " << reversed[0] << std::endl;
42+
std::cout << "The original value has *not* been updated though the value!" << std::endl;
43+
}
File renamed without changes.

Lesson 2/Activity 7/compile.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build and link main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 2/Activity 7/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Open a namespace called 'a'
2+
namespace a {
3+
4+
// Define a function named 'a' inside the namespace 'a'
5+
void a() {}
6+
7+
// The namespace 'a' is closed here
8+
}
9+
10+
// Open a namespace called 'b'
11+
namespace b {
12+
13+
// Define a function named 'b' inside the namespace 'b'
14+
void b() {}
15+
16+
// The namespace 'b' is closed here
17+
}
18+
19+
// Open an anonymous namespace
20+
namespace {
21+
22+
// Define a function named 'c' inside the anonymous namespace
23+
void c() {
24+
// To call functions inside a namespace different from the current functions you
25+
// need to put the name of the namespace first.
26+
a::a();
27+
using b::b;
28+
b();
29+
}
30+
31+
// The anonymous namespace is closed here
32+
}
33+
34+
// Here we are outisde of any namespace
35+
36+
int main() {
37+
// The c() function is callable from outside the anonymous namespace.
38+
c();
39+
b::b();
40+
}

Lesson 2/Activity 8/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# BeginningCPlusPlus

Lesson 2/Activity 8/compile.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build and link main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 2/Activity 8/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
3+
int square(int x) {
4+
return x * x;
5+
}
6+
7+
float square(float x) {
8+
return x * x;
9+
}
10+
11+
int main() {
12+
// The overload which takes float is called
13+
std::cout << square(1.5f) << std::endl;
14+
// The overload which takes int is called
15+
std::cout << square(1) << std::endl;
16+
}

Lesson 2/Exercise 4/compile.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build and link main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 2/Exercise 4/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
3+
// Define and declares the function.
4+
// The parameter is taken by value: a new copy will be created.
5+
// Changes inside the function will not be reflected in the value
6+
// used when calling the function
7+
void byvalue_age_in_5_years(int age) {
8+
// Age here is a new copy. We increment it by 5 but then it's lifetime terminates.
9+
age += 5;
10+
}
11+
12+
int main() {
13+
int a = 95;
14+
byvalue_age_in_5_years(a);
15+
// a is not modified because it was accepted by value
16+
std::cout << "Value: " << a; // Prints 95
17+
}

Lesson 2/Exercise 5/compile.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:: Folder where the built objects are stored
2+
mkdir build
3+
4+
:: Build and link main.cpp
5+
cl /EHsc /Fobuild/ /Febuild/ main.cpp
6+
7+
:: You can not run the program at build\main.exe

Lesson 2/Exercise 5/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
3+
// The parameter is taken by reference.
4+
// Changes inside the function will be reflected in the value of the caller
5+
void byvalue_age_in_5_years(int& age) {
6+
age += 5;
7+
}
8+
9+
int main() {
10+
int a = 95;
11+
byvalue_age_in_5_years(a);
12+
// a has been incremented by the function, because it operated on a reference
13+
// to the local variable
14+
std::cout << "Value: " << a; // Prints 100
15+
}

0 commit comments

Comments
 (0)