💻 Computer Science
📖 Table of Contents
The C++ Project
Learning a legendary language by simulating the physical world
This entry is a companion article written as I go through a book written by Marco Tewfiq called “The C++ Project” The goal of this book is to compliment the process of learning C++ by implementing a physics-based collision simulator. I love the idea of learning something about the natural world by leveraging the computer and learning how to program as a side effect. C++ has been called the “latin of programming” - my feeling is understanding this language will provide not only context, but deep appreciation for some of the other higher level languages that are pervasive in the industry while making it more clear what certain abstractions are doing under the hood.
You can check out the book on Amazon
🌱 This is a living post. My personal notes as I make my way through the book There will be many examples, explanations and thoughts. Please make use of the Table of Contents in the upper right to find what is of interest to you.
This book is trying to teach us about the laws of physics, using C++ to illustrate concepts. Before we dive into some practical examples, let’s get a feel for the basic syntax of C++.
Variables
C++ is strongly and statically typed
.
We must declare variables prefaced by their type.
Types must be known at compile time.
int num = 32;
float flt = 32.33;
std::string str = 'Hello world.'
Pointers
The whole point of using a low level language like C++ is to give the developer direct access
to concepts that closely mirror the computer’s reality in a way that humans can succinctly express. This means we have fine
grained control over things like memory management
, down to the exact bit where something is stored.
Pointers specify that a variable (declared with *
) will be a reference to the memory address of the pointee.
int a = -1;
int* b;
b = &a; // 0x7fff5b7bff84 - memory address
The following example is not valid and will result in a compilation error.
When initalizing a pointer, you must use the “address-of” operator &
.
int a = 1;
int*b;
b = a;
This is not valid either. We are declaring b
as a type int
, but not as a pointer to an int
value.
&a
is going to return an address in memory, therefore we have a type mismatch and your program will not compile.
int a = 1;
int b;
b = &a;
*
- “this variable will point to the memory address where the value is stored”
&
- “get the memory address where this variable is located”
Functions
Functions and their arguments are also prefaced by type.
Let’s define a function that gets the distance between two points on a 2D plane.
Notice we specify the return type, in this case double
, before the function declaration.
#include <cmath> // import the std C math library -> sqrt() and pow()
double distanceToPoint(double x1, double y1, double x2, double y2) {
double distance;
distance = sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0));
return distance;
}
This is the computer program equivelant of:
Since our simulation will be using the laws of physics, we will be seeing many mathmatical equations throughtout this. We will get very good at representing mathmatical concepts using programming constructs and observe that there are indeed many correlates.
All C++ programs use main()
as their entry point.
#include <iostream>
#include <cmath>
double distanceToPoint(double x1, double y1, double x2, double y2) {
double distance = sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0));
return distance;
}
int main() {
double x1 = 1.0, y1 = 2.0;
double x2 = 4.0, y2 = 6.0;
double result = distanceToPoint(x1, y1, x2, y2);
std::cout << "The distance between point (" << x1 << ", " << y1 << ") and point (" << x2 << ", " << y2 << ") is " << result << std::endl;
return 0;
}
🧠 The conventionin C++ is to return an
int
frommain()
. A0
is used to represent that the program completed succesfully, while other integers can signify errors.
Structs
Structs are compound data types that allow us to group related information together.
struct point {
int x;
int y;
}
point p1;
p1.x = 0;
p1.y = 0;
point p2, p3;
p2.x = 1;
p2.y = 1;
p3 = { 2, 2 } // initalize using a brace-enclosed list of values
Classes
Classes are a more elaborate compound data type. They can implement functions, otherwise known as class methods.
We can control access to data members by using public
and private
properties.