A short introduction to C++ - basic

 

Part 0. Setting up C++

OS: Linux / Mac OS

We are going to have two windows open at the same time:

  1. A text editor (vi, emacs, vscode etc).

  2. The command line or terminal, where a C++ compiler has already been installed and is working.

Part 1. Hello world!

1. Getting ready

  1. Create a new directory (folder) in your working directory called cpp by typing mkdir cpp.

  2. Change directory to your new cpp directory by typing cd cpp.

2. Hello world!

For cin cout endl stuff, we need the <iostream> library. Libraries are like toolboxes.

3. Use of variables

4. Command-line input

5. Maths

6. If

7. For and while loops

 

Part 2. Files, arrays and functions

8. Saving and reading data

To save and read files, we need to include the <fstream> library.

Your turn:

9. Arrays

10. Functions

Writing functions allows us to do more complicated things.

 

 

Part 3. Pointers, classes

11. Pointers

12. Main function with arguments

If you want to access command line arguments you can declare main() as follows

New file! main_arg.cpp

outputs:

13. Classes

Classes are one of the main things that separate C++ from C. Classes are ways for us to set up objects that have properties and functions which are particular to that type of object. A class is something like a userdefined data type.

You should get something like:

 

If you want to use Rectangle in lots of files, it's annoying to define it every time.