Skip to content

Learning C++ from scratch

Why

I've wanted to learn C or C++ for some years.

C is to modern programming languages as Latin is to modern European languages, except that C is still alive and thriving as a fast, light weight systems programming language. C was created by Dennis Ritchie at Bell Labs in the 1970, and based on the B language. His collaborator at Bell Labs, Ken Thompson, wrote the UNIX operating system in C. Because you needed to use C to maintain your operating system, the C programming language became very popular, and it still is. C++ was created by Bjarne Stroustrup in the 1980, as a way to add Object-Oriented features to the C language.

My Master's thesis advisor said that you get much finer control over the computer using C rather than something like Python. And C++ is necessary for high-performance microcontroller code.

Right now I want to learn C++ in order to better understand the SimpleFOC code that I use in Baksi and the BLDC Modular Thing.

I'll keep my programs in this repo:

GitHub logo My Learning C++ GitHub repository

Failed attempt

I tried to follow a C++ programming book some years back, I don't remember which one it was. I installed an IDE on my Windows laptop and tried to add something or other to PATH, but I couldn't get anything to compile and I didn't know why. I'm writing my journey down this time, in case it helps someone out.

Successful setup

If you want to try running C++ code right away, you can copy one of the examples on this page into OnlineGDB and compile and run it in your browser.

To compile and run C++ code, I installed Linux Mint inside VirtualBox, which was easy (here's a tutorial). Then I found a short video showing how to compile a C++ program in Linux Mint using the Code::Blocks editor. It worked!

Note

I later found out that you can install the Code::Blocks editor on Windows and Mac. On those operating systems, you need to install GCC yourself.

Here's what you type into the Linux terminal to get started:

sudo apt-get update
sudo apt-get install codeblocks
gcc --version
That's it! I only updated my system, installed Code::Blocks and checked the GCC version. Conveniently, GCC comes preinstalled on most Linux distributions. If your GCC version is 11.3.0 or later, you're good to go. Open the Code::Blocks editor, select the GCC compiler and then create a new project. You want to create a Console application. That's it, now you can write your first C++ program:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!\n";
    return 0;
}
In the top menu, select Build -> Build and run. The output is

Hello world!

Yay!

I also found a tutorial that shows how to compile and run a C++ program in the Linux terminal. Nice! All you have to do is navigate to the right folder using cd and then enter

g++ cplus.cpp
./a.out
Then the program runs in the Linux terminal.

The C++ book

Problem solving with C++

My father-in-law handed me a used copy of Problem Solving with C++ by Walter Savitch at the yearly bazaar in Hnífsdalur in November 2023. The book only cost ISK 100 ($1), so I bought it. I have the seventh edition. The book is friendly and accessible. It assumes that you have never programmed a computer before and you only need to understand the most basic high school algebra for the more involved exercises.

Alternatively you can go through the tutorials at LearnCpp.com. They seem really good.

But I'm going ahead with the book:

Chapter 1

Freefall

After Hello world, I made a slightly more useful program (since I'm teaching physics at ):

#include <iostream>
using namespace std;

int main()
{
    double t, y;
    cout << "Enter freefall time in seconds: " << endl;
    cin >> t;
    y = 9.81*t*t/2;
    cout << "The vertical distance covered is " << y << " meters." << endl;
    return 0;
}
When I run it, it looks like this:

Enter freefall time in seconds: 5
The vertical distance covered is 122.625 meters.
Nice!

Terminal graphics

Then I tried my hand at making ASCII graphics:

#include <iostream>

using namespace std;

int main()
{
    cout << "**********************************" << endl;
    cout << "  BBB       A     K  K   SSS   I  " << endl;
    cout << "  B  B     A A    K K    S     I  " << endl;
    cout << "  BBB     A   A   K      SSS   I  " << endl;
    cout << "  B  B    AAAAA   K K      S   I  " << endl;
    cout << "  BBB     A   A   K  K   SSS   I  " << endl;
    cout << "**********************************" << endl;
    return 0;
}
The result:
**********************************
  BBB       A     K  K   SSS   I  
  B  B     A A    K K    S     I  
  BBB     A   A   K      SSS   I  
  B  B    AAAAA   K K      S   I  
  BBB     A   A   K  K   SSS   I  
**********************************
This is fun!