You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 01projects/sec02CppSyntax.md
+31-6Lines changed: 31 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,14 +77,39 @@ double y = 7;
77
77
78
78
Here the compiler will interpret the literal `7` as an `int` and then convert it to `double` to be assigned to the variable `y`.
79
79
80
+
## Comments
81
+
82
+
Comments are extremely useful for explanatory notes in your code. You can begin a comment with `//` for a comment on a single line.
83
+
84
+
```cpp
85
+
// This is a comment.
86
+
87
+
double r = 5.0; // Comments can also be on the same line as some code.
88
+
```
89
+
90
+
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.
91
+
92
+
```cpp
93
+
/* This is a longer comment
94
+
over multiple lines. The code in this comment will not execute.
95
+
96
+
int x = 5*5;
97
+
int y = x + 17;
98
+
99
+
*/
100
+
101
+
int z = 2*12; // this code does execute.
102
+
```
103
+
104
+
Be careful when commenting out code that you don't comment out definitions that you are relying on elsewhere!
105
+
80
106
## Defining and Calling Functions
81
107
82
108
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.
83
109
84
110
```cpp
85
111
#include<iostream>
86
112
87
-
//
88
113
intsquare(int x)
89
114
{
90
115
return x*x;
@@ -121,7 +146,7 @@ This would of course be a waste of time with a function like this, but is occasi
121
146
122
147
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.)
123
148
124
-
```cpp=
149
+
```cpp
125
150
// if statement with a Boolean variable
126
151
if(condition)
127
152
{
@@ -144,7 +169,7 @@ In the examples above, nothing will happen if the statement inside the brackets
144
169
145
170
If you want something to happen when the statement is false, you can also use `else` and/or `else if` statements.
146
171
147
-
```cpp=
172
+
```cpp
148
173
if(x < 10)
149
174
{
150
175
std::cout << "x is small" << std::endl;
@@ -161,7 +186,7 @@ else
161
186
162
187
## Loops (`for` and `while`)
163
188
164
-
```cpp=
189
+
```cpp
165
190
for(unsignedint i = 0; i < 100; ++i)
166
191
{
167
192
// loop code goes here
@@ -177,7 +202,7 @@ for(unsigned int i = 0; i < 100; ++i)
177
202
-`++i` increments the value of `i` by 1.
178
203
179
204
If we have a `vector` or similar container, we can loop over its elements without writing our own loop conditions:
180
-
```cpp=
205
+
```cpp
181
206
#include<vector>
182
207
183
208
intmain()
@@ -198,7 +223,7 @@ int main()
198
223
199
224
`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.
Copy file name to clipboardExpand all lines: 01projects/sec03MultipleFiles.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ title: C++ Programs with Multiple Files
7
7
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`.
8
8
9
9
**main.cpp:**
10
-
```cpp=
10
+
```cpp
11
11
#include<iostream>
12
12
13
13
intmain()
@@ -22,7 +22,7 @@ int main()
22
22
```
23
23
24
24
**function.cpp:**
25
-
```cpp=
25
+
```cpp
26
26
intf(int a, int b)
27
27
{
28
28
return (a+2) * (b-3);
@@ -50,7 +50,7 @@ Let's use this simple example program to explore how the compiler deals with our
50
50
We'll start with a single file and work towards a multiple file version. Consider the following two versions of the same program:
51
51
52
52
**Version 1**
53
-
```cpp=
53
+
```cpp
54
54
#include <iostream>
55
55
56
56
int f(int a, int b)
@@ -70,7 +70,7 @@ int main()
70
70
```
71
71
72
72
**Version 2**
73
-
```cpp=
73
+
```cpp
74
74
#include<iostream>
75
75
76
76
intmain()
@@ -93,7 +93,7 @@ Only the first of these two programs will compile!
93
93
- 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.
94
94
95
95
**With a function declaration:**
96
-
```cpp=
96
+
```cpp
97
97
#include <iostream>
98
98
99
99
// 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
129
129
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.
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.
178
+
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.
179
179
180
180
In this case the files look as follows:
181
181
182
182
**function.h**:
183
-
```cpp=
183
+
```cpp
184
184
intf(int a, int b); // function declaration
185
185
```
186
186
187
187
**function.cpp**:
188
-
```cpp=
188
+
```cpp
189
189
int f(int a, int b)
190
190
{
191
191
return (a + 2) * (b - 3);
192
192
}
193
193
```
194
194
195
195
**main.cpp**:
196
-
```cpp=
196
+
```cpp
197
197
#include<iostream>
198
198
#include"function.h"// include our header file with the declaration
0 commit comments