Skip to content
Merged
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
1 change: 0 additions & 1 deletion 01projects/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 31 additions & 6 deletions 01projects/sec02CppSyntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,39 @@ 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.

```cpp
#include <iostream>

//
int square(int x)
{
return x*x;
Expand Down Expand Up @@ -121,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)
{
Expand All @@ -144,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;
Expand All @@ -161,7 +186,7 @@ else

## Loops (`for` and `while`)

```cpp=
```cpp
for(unsigned int i = 0; i < 100; ++i)
{
// loop code goes here
Expand All @@ -177,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 <vector>

int main()
Expand All @@ -198,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;
Expand Down
22 changes: 11 additions & 11 deletions 01projects/sec03MultipleFiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>

int main()
Expand All @@ -22,7 +22,7 @@ int main()
```

**function.cpp:**
```cpp=
```cpp
int f(int a, int b)
{
return (a+2) * (b-3);
Expand Down Expand Up @@ -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 <iostream>

int f(int a, int b)
Expand All @@ -70,7 +70,7 @@ int main()
```

**Version 2**
```cpp=
```cpp
#include <iostream>

int main()
Expand All @@ -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 <iostream>

// Function declaration for f
Expand Down Expand Up @@ -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 <iostream>

int f(int a, int b);
Expand All @@ -145,7 +145,7 @@ int main()
```

**function.cpp**:
```cpp=
```cpp
int f(int a, int b)
{
return (a + 2) * (b - 3);
Expand Down Expand Up @@ -175,25 +175,25 @@ 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:

**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);
}
```

**main.cpp**:
```cpp=
```cpp
#include <iostream>
#include "function.h" // include our header file with the declaration

Expand Down