C++ Programming For Blockchain Developers
C++ Programming For Blockchain Developers
– MIT Lecture
https://www.youtube.com/watch?v=k6U-i4gXkLM
1.2 — Comments
A comment is a programmer-readable note that is inserted directly into the source code of the program.
Comments are ignored by the compiler and are for the programmer’s use only.
To comment out a single line of code, simply use the // style comment to turn a line of code into a comment
temporarily:
Uncommented out:
1 std::cout << 1;
Commented out:
1 // std::cout << 1;
To comment out a block of code, use // on multiple lines of code, or the /* */ style comment to turn the block
of code into a comment temporarily.
Uncommented out:
1 std::cout << 1;
2 std::cout << 2;
3 std::cout << 3;
Commented out:
1 // std::cout << 1;
2 // std::cout << 2;
3 // std::cout << 3;
or
1 /*
2 std::cout << 1;
3 std::cout << 2;
4 std::cout << 3;
5 */
Here are some examples of bad line comments and good statement comments.
Bad comment:
2 sight = 0;
Reason: We already can see that sight is being set to 0 by looking at the statement
Good comment:
// The player just drank a potion of blindness and can not see
1
anything
2
sight = 0;
Bad comment:
Reason: We can see that this is a cost calculation, but why is quantity multiplied by 2?
Good comment:
Programmers often have to make a tough decision between solving a problem one way, or solving it another
way. Comments are a great way to remind yourself (or tell somebody else) the reason you made one decision
instead of another.
Good comments:
Finally, comments should be written in a way that makes sense to someone who has no idea what the code
does. It is often the case that a programmer will say “It’s obvious what this does! There’s no way I’ll forget
about this”. Guess what? It’s not obvious, and you will be amazed how quickly you forget. :) You (or
someone else) will thank you later for writing down the what, how, and why of your code in human
language. Reading individual lines of code is easy. Understanding what goal they are meant to accomplish is
not.
Variables in C++
Just like Javascript, C++ has the notion of variables. Declaring and using variables is a bit different from
Javascript though and in this lecture we will learn exactly how variables work in C++.
Read this article (http://www.learncpp.com/cpp-tutorial/13-a-first-
look-at-variables-initialization-and-assignment/).
int x; // define a variable named x, of type
1
int
At compile time, when the compiler sees this statement, it makes a note to itself that we are defining a
variable, giving it the name x, and that it is of type int (more on types in a moment). From that point forward
(with some limitations that we’ll talk about in a future lesson), whenever the compiler sees the identifier x, it
will know that we’re referencing this variable.
Value initialization and zero initialization
When a variable is initialized with empty braces, value initialization takes place. In most cases, value
initialization will initialize the variable to zero (or empty, if that’s more appropriate for a given type). In such
cases where zeroing occurs, this is called zero initialization.
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es20-always-initialize-
an-object
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/variables-in-c-reading-assignment/
3160).
Functions in C++
Let’s move on and learn about functions!
1. What is a function?
2. What function is run first of all in a C++ program?
3. What are return values?
4. Are nested functions allowed in C++?
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/functions-in-c-reading-assignment/
3161).
1. What is a function?
A piece of code with a name (you can call that function by that name() ) a return values, and some
parameters
2. What function is run first of all in a C++ program?
main
3. What are return values?
A function can return values, like a int , string, etc. Eventually it can return nothing (void)
4. Are nested functions allowed in C++?
No
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/functions-and-parameters-c-
reading-assignment/3162).
1. What is an array?
2. Which index would you use to access the 2nd variable in an array?
3. What can you say about the size of an array?
Btw, you may have heard about the EOS Vulnerability, it occured due to writing outside of the array
bounds (watch here: https://www.youtube.com/watch?v=37H1PJMoN3M).
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/arrays-in-c-reading-assignment/316
4).
1. What is an array?
An array is a struct that holds many vars with the same type and identified by an index
2. Which index would you use to access the 2nd variable in an array?
1
3. What can you say about the size of an array?
It has to be stated when you declare the array and the size has to be known at compile time.
size(array)
3. How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array) / sizeof(array[0])
4. What happens when you try to write outside the bounds of the array?
You write on stack (memory space). "stack smashing detected". Dangerous
Loops in C++
Let’s move on and learn about loops!
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned
previously?
2. How would you write a for-loop in code?
3. What is an off-by-one error?
Post your answers and feel free to discuss the quizes in this thread
(https://forum.ivanontech.com/t/loops-in-c-reading-assignment/3168
).
1. What can we do with the while statement?
Make a loop based on a condition
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
true
3. What is an infinite loop?
A loop that keeps iterating cause the condition is always true.
while (true) {.....}
4. What is an iteration?
Repetition of a bunch of instruction
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned
previously?
When there is an obvious loop variable
2. How would you write a for-loop in code?
for (signed int i {0}; i<10; ++i) { do something }
3. What is an off-by-one error?
It happens when you do a mistake with the condition in the for loop.
Like
for (int i {0}; i<=10; ++i) {}
This will loop 11 times: did you mean 10?
#include <iostream>
#include <string>
/*
GUESS THE NUMBER
1) Player 1 choose a number
2) Player 2 try to guess the number
3) Player 1 will tell if the guessed number is lower or higher or right!
*/
// welcome banner
void banner() {
cout <<
"GUESS THE NUMBER" << "\n\n"
"1) Player 1 (played by computer) choose a number" << "\n"
"2) Player 2 try to guess the number" << "\n"
"3) Player 1 will tell if the guessed number is lower or higher or right!" << "\n"
<< "\n";
}
int main() {
// number of guessing attempts
signed int moves {0};
// guess flag
bool guessed {false};
// games log
struct Games {
signed int moves;
signed int guess;
string msg;
};
// Welcome
banner();
Structs in C++
Let’s move on and learn about functions!
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/structs-c-reading-assignments/3169)
.
• Why do we need structs?
A struct (short for structure) allows us to group variables of mixed data types together into a single
unit
• What are members in a struct?
These variables that are part of the struct are called members (or fields)
• How do you access a member inside a struct?
In order to access the individual members, we use the member selection operator (which is a period)
struct Employee
{
int id{};
int age{};
double wage{};
};
Employee joe; // create an Employee struct for Joe
joe.id = 14; // assign a value to member id within struct joe
joe.age = 32; // assign a value to member age within struct joe
joe.wage = 24.15; // assign a value to member wage within struct joe
struct Employee
{
int id{};
int age{};
double wage{};
};
struct Company
{
Employee CEO{}; // Employee is a struct within the Company struct
int numberOfEmployees{};
};
Company myCompany;
Classes in C++
Let’s move on and learn about classes!
At the bottom of the article you’ll see a quiz – make sure to do the
quiz!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/classes-in-c-reading-assignment/317
0).
1. Why do we need classes, why can’t we just use structs?
For convention structs are for data, classes are for data and functions.
2. What are methods?
Methods are the function in a class
3. How would you define a class?
With the key word class (it is similar to struct).
Like so:
class Ball {
public:
double size;
string color;
void print() {
std::cout << "ball info: " << "\n" ;
std::cout << " size: " << size << "\n";
std::cout << " color: " << color << "\n";
};
}
4. What is an object?
An object is an instance of a class
Inheritance in C++
Let’s move on and learn about inheritance!
Post your answers and feel free to discuss the quiz in this thread
(https://forum.ivanontech.com/t/inheritance-in-c-reading-
assignment/3172).
1. Why do we need inheritance in C++?
Inheritance allow us to create objects that directly inherits properties of another objects. And so, we don’t
have to redefine some properties in our derived class.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
A vehicle can be the base class for a car a truck etc…
typedef in C++
typedef double height;
Template Functions
Template function give flexibility about the type of parameters and return value of a function. In this
example multiply can be used for int and double.
#include <iostream>
int main() {
cout << multiply(10,2) << '\n';
cout << multiply(10.5,2.5) << '\n';
//cout << multiply(10,2.5) << '\n'; // This doesn't work you cannot mix types (T can't be int and
double at the same time)
}
#include <iostream>
int main() {
// You can define intMathTools with explicit type or define a typedef for shorter and cleaner
code.
//MathTools<int> intMathTools;
IntTools intMathTools;
//MathTools<double> doubleMathTools;
DoubleTools doubleMathTools;
#include <iostream>
int main() {
int val {1};
OUTPUT:
val FIRST = 1
val INSIDE incVal = 2
val after incVal = 1
val INSIDE incReference = 2
val after incReference = 2
Pointers C++
Pointers are data type that holdes the address as his value.
You can define a pointer using '*':
int age { 51 };
// int* define a pointer (to a int)
// &var means the address of var
// ptr is a pointer to age (type int)
int* ptr {&age};
// (*ptr) means the int var that ptr points to
(*ptr)++; // 52
Test program:
#include <iostream>
// int*
void inc(int* val) {
(*val)++;
}
int main() {
int age { 51 };
printAge(age);
// &var means the address of var
cout << The address of age is << &age << n;
}
OUTPUT
My age is 51
The address of age is 0x7fffae64b2ec
The pointer is 0x7fffae64b2ec
My age is 52
My age is 53
My age is 54
https://dev.to/talhabalaj/setup-visual-studio-code-for-multi-file-c-projects-1jpi
├── bin
├── include
└── src
The bin directory will contain the executable after compilation, the include directory will contain all the
header files, src will contain all the (as you have guessed) source files.
Makefile is read by the make utility which executes the tasks defined in Makefile when the files are
modified. We will be using it to build our C++ code. You can learn more about Makefile and make here.
For our purposes, following Makefile should work well. so create a Makefile in the root of your project
directory. It should spell exactly the same with a capital M.
CXX := g++
CXX_FLAGS := -std=c++17 -ggdb
BIN := bin
SRC := src
INCLUDE := include
LIBRARIES :=
EXECUTABLE := main
all: $(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBRARIES)
clean:
-rm $(BIN)/*
If you want to add additional libraries, you can add it after the LIBRARIES variable. You can also change
the name of the executable by changing the EXECUTABLE variable. You can tinker with CXX_FLAGS to
change the compiler's behavior (i.e. C++ version) but be sure you don't remove -ggdb flag or you won't be
able to debug the program correctly.
After creating the file, our directory structure should look like this:
├── bin
├── include
├── lib
├── Makefile
└── src
Now that we have our base ready let's add some C++ files. I will create a HelloWorld class that has
a say() function that will print out (you guessed it) "Hello, World". Let's create files in our project. I'm
gonna use the command line, you can use VSCode or your file manager.
#include <iostream>
class HelloWorld {
public:
void say();
};
#include "HelloWorld.hpp"
void HelloWorld::say() {
std::cout << "Hello, World";
}
Add following to main.cpp.
#include "HelloWorld.hpp"
int main() {
HelloWorld world;
world.say();
return 0;
}
~/Documents/cpp_tut
▶ make
g++ -std=c++17 -ggdb -Iinclude src/HelloWorld.cpp src/main.cpp -o bin/main
Now, in the output, we will say the exact command that we would have run if we weren't
using make. make has got our back. We don't have to write the whole command each time.