From 7e2ba26d85146bec873fb96f60457a05dddd979b Mon Sep 17 00:00:00 2001 From: Michael McLeod Date: Wed, 26 Nov 2025 16:29:50 +0000 Subject: [PATCH 1/5] Remove terminal from index --- 01projects/index.md | 1 - 1 file changed, 1 deletion(-) diff --git a/01projects/index.md b/01projects/index.md index c2d705db4..d2775c168 100644 --- a/01projects/index.md +++ b/01projects/index.md @@ -13,7 +13,6 @@ In this section we also provide notes on how to use the **command line terminal* - [Introduction to C++ and "Hello World" Program](./sec01IntroToCpp.md) - [Further C++ Syntax](./sec02CppSyntax.md) - [C++ Programs with Multiple Files](./sec03MultipleFiles.md) -- [Using the terminal](./sec04Terminal.md) - [Version control with Git](./sec05Git.html) ## Useful References From e0bf2e72f64ffea121541ff6030dc514df83378f Mon Sep 17 00:00:00 2001 From: Michael McLeod Date: Wed, 26 Nov 2025 17:06:27 +0000 Subject: [PATCH 2/5] Add discussion of comments --- 01projects/sec02CppSyntax.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/01projects/sec02CppSyntax.md b/01projects/sec02CppSyntax.md index 944a704d7..1065d7e7e 100644 --- a/01projects/sec02CppSyntax.md +++ b/01projects/sec02CppSyntax.md @@ -77,6 +77,32 @@ double y = 7; Here the compiler will interpret the literal `7` as an `int` and then convert it to `double` to be assigned to the variable `y`. +## Comments + +Comments are extremely useful for explanatory notes in your code. You can begin a comment with `//` for a comment on a single line. + +```cpp +// This is a comment. + +double r = 5.0; // Comments can also be on the same line as some code. +``` + +Longer comments begin with `/*` and can stretch over multiple lines, until we reach `*/`. This can be used for long comments or to temporarily comment out functionality. + +```cpp +/* This is a longer comment +over multiple lines. The code in this comment will not execute. + +int x = 5*5; +int y = x + 17; + +*/ + +int z = 2*12; // this code does execute. +``` + +Be careful when commenting out code that you don't comment out definitions that you are relying on elsewhere! + ## Defining and Calling Functions We've already seen function signatures and the `main` function, let's look at how to define a simple function which will get used in our main. @@ -84,7 +110,6 @@ We've already seen function signatures and the `main` function, let's look at ho ```cpp #include -// int square(int x) { return x*x; From 6df4c12d0984e2a0645d69e5806efb29cdc7ca93 Mon Sep 17 00:00:00 2001 From: Michael McLeod Date: Wed, 26 Nov 2025 17:07:52 +0000 Subject: [PATCH 3/5] Fixing syntax highlighting --- 01projects/sec02CppSyntax.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/01projects/sec02CppSyntax.md b/01projects/sec02CppSyntax.md index 1065d7e7e..f03d2d5df 100644 --- a/01projects/sec02CppSyntax.md +++ b/01projects/sec02CppSyntax.md @@ -146,7 +146,7 @@ This would of course be a waste of time with a function like this, but is occasi A true/false value is called a Boolean value, or `bool`. Conditional statements test the value of a Boolean value or expression and execute the following code block if it is `true`. (Remember that a code block is contained within curly braces `{}`, and can be as large as you like.) -```cpp= +```cpp // if statement with a Boolean variable if(condition) { @@ -169,7 +169,7 @@ In the examples above, nothing will happen if the statement inside the brackets If you want something to happen when the statement is false, you can also use `else` and/or `else if` statements. -```cpp= +```cpp if(x < 10) { std::cout << "x is small" << std::endl; @@ -186,7 +186,7 @@ else ## Loops (`for` and `while`) -```cpp= +```cpp for(unsigned int i = 0; i < 100; ++i) { // loop code goes here @@ -202,7 +202,7 @@ for(unsigned int i = 0; i < 100; ++i) - `++i` increments the value of `i` by 1. If we have a `vector` or similar container, we can loop over its elements without writing our own loop conditions: -```cpp= +```cpp #include int main() @@ -223,7 +223,7 @@ int main() `while` loops have simpler syntax than `for` loops; they depend only on a condition, and the code block executes over and over until the condition is met. This is useful for situations where the number of iterations is not clear from the outset, for example running an iterative method until some convergence criterion is met. -```cpp= +```cpp while( (x_new - x_old) > 0.1) // convergence criterion { x_old = x_new; From 4165f1b3ccae858cac48cfae30c030668a486734 Mon Sep 17 00:00:00 2001 From: Michael McLeod Date: Wed, 26 Nov 2025 17:09:25 +0000 Subject: [PATCH 4/5] Fix syntax highlighting --- 01projects/sec03MultipleFiles.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/01projects/sec03MultipleFiles.md b/01projects/sec03MultipleFiles.md index c4f65480e..48da5d024 100644 --- a/01projects/sec03MultipleFiles.md +++ b/01projects/sec03MultipleFiles.md @@ -7,7 +7,7 @@ title: C++ Programs with Multiple Files Like other programming languages, it is possible (and good practice!) to break up C++ programs into multiple files. C and C++ however have a slightly unusual approach to this compared to some other languages. Let's consider that we have two C++ files, our `main.cpp` which contains our `main` function (the entry point for execution of our program), and another which defines some function that we want to use in `main`. **main.cpp:** -```cpp= +```cpp #include int main() @@ -22,7 +22,7 @@ int main() ``` **function.cpp:** -```cpp= +```cpp int f(int a, int b) { return (a+2) * (b-3); @@ -50,7 +50,7 @@ Let's use this simple example program to explore how the compiler deals with our We'll start with a single file and work towards a multiple file version. Consider the following two versions of the same program: **Version 1** -```cpp= +```cpp #include int f(int a, int b) @@ -70,7 +70,7 @@ int main() ``` **Version 2** -```cpp= +```cpp #include int main() @@ -93,7 +93,7 @@ Only the first of these two programs will compile! - C++ does **not** need to know everything about `f` ahead of time though; it just need to know _what_ it is and what its type is. This is the job of **forward declaration**: something that tells us that there will be a function with this signature defined somewhere in the program is without telling us exactly what it does. We can also have declarations for things other than functions in C++, as we shall see later on in the course. **With a function declaration:** -```cpp= +```cpp #include // Function declaration for f @@ -129,7 +129,7 @@ This might seem like a rather pointless thing to do in a program as trivial as t Now that we know that we can write function declarations, we can move the function definition to a different file, and compile both files separately. **main.cpp**: -```cpp= +```cpp #include int f(int a, int b); @@ -145,7 +145,7 @@ int main() ``` **function.cpp**: -```cpp= +```cpp int f(int a, int b) { return (a + 2) * (b - 3); @@ -180,12 +180,12 @@ Forward declarations for functions are helpful, but they can still clutter up ou In this case the files look as follows: **function.h**: -```cpp= +```cpp int f(int a, int b); // function declaration ``` **function.cpp**: -```cpp= +```cpp int f(int a, int b) { return (a + 2) * (b - 3); @@ -193,7 +193,7 @@ int f(int a, int b) ``` **main.cpp**: -```cpp= +```cpp #include #include "function.h" // include our header file with the declaration From 7b9a53f80afe40c23367c904b8a3549c5ac78654 Mon Sep 17 00:00:00 2001 From: Michael McLeod Date: Wed, 26 Nov 2025 17:11:04 +0000 Subject: [PATCH 5/5] Clarifying why headers are useful --- 01projects/sec03MultipleFiles.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01projects/sec03MultipleFiles.md b/01projects/sec03MultipleFiles.md index 48da5d024..55bcb4915 100644 --- a/01projects/sec03MultipleFiles.md +++ b/01projects/sec03MultipleFiles.md @@ -175,7 +175,7 @@ g++ -o test_f main.cpp function.cpp ## Header Files -Forward declarations for functions are helpful, but they can still clutter up our code if we are making use of large numbers of functions! Instead, we put these declarations in **header files**, which usually end in `.h` or `.hpp`. We use `#include` to add header files to a `.cpp` file: this allows the file to get the declaration from the header file. The definitions are not kept in the header file, they are in a separate `.cpp` file. +Forward declarations for functions are helpful, but they can still clutter up our code if we are making use of large numbers of functions. We would also need to rewrite these forward declarations for _every_ source file that needs to use them! Instead, we put these declarations in **header files**, which usually end in `.h` or `.hpp`. We use `#include` to add header files to a `.cpp` file: this allows the file to get all the declaration from the header file. The definitions are not kept in the header file, they are in a separate `.cpp` file so that they can be compiled separately. In this case the files look as follows: