Skip to content

C++ Crash Course - Ch 2 - TYPES


Integers

  • short
  • int
  • long
  • long long

Also unsigned versions of the above.

The size of these is not guaranteed across platforms. To use guaranteed sizes you need #include <cstdint> and then you can us int8_t, int16_t, int32_t, int64_t. However #include <cstdint> is not available on every platform.

base prefix
binary 0b
octal 0 - this can cause issues with literals that have leading 0's
hex 0x

Can intersperse literal numbers with ' for example int mill = 1'000'000


Floats

  • float
  • double
  • long double

literal values default to double. e.g.

float a = 3.142f;
double b = 3.142;
long double = 3.142L;

Chars

standard wide
char wchar_t
signed char char16_t
unsigned char char32_t

Literals - use the L prefix for wide chars:-

char a = 'j';
wchar_t = L'j';

Other Types

std::byte

an 8 bit value. Cannot use arithmetic operations but can use bitwise.

size_t

held in the <cstddef> header. the print specifier is %zu

printf("size of float is %zu", sizeof(float))

Initialising

default (set to zero) other
int a= 0; int a = 42;
int a { }; int a {42}; // pretty much always applicable - preferred method
int a = { }; int a= {42};
int a; (1) int a(42); // looks like a function -> not good

(1) not guaranteed to be set to zero!


Arrays

int array1[100];
int array2 = { 1 ,2, 3, 4, 5};

Can find the number of elements in an array with this (considered to be a bit old school hackery):-

int n_elements = sizeof(array2) / sizeof(array2[0]);

Initialising an Array

int array1[] { 1, 2, 3 };  // array length is 3, contents are 1, 2, 3
int array2[5] {};          // array length is 5, contents all set to zero
int array3[5] { 1, 2, 3 }; // array length is 5, contents are 1, 2, 3, 0, 0
int array4[5];             // array length is 5, contents are uninitialised

Looping through arrays

Using an index

for(size_t i = 0; i<n_elements; i++)
{
    // do stuff with array2[i];
}

The range-based loop

for(int val : array2) {
    //do stuff with val
}

Note: the range based-loop cannot be used to update the values held in the array as the value used is a copy of the value from the array.

However, by using references you can update the value...

for(int& val : array2) {
    //do stuff TO val
}

Strings

CStrings are declared with...

char my_string[] = "This is a journey "
                   "into sound";

Note: the string literal can be split across lines and the compiler will treat it as a single uninterrupted string.


Enums

enum class Computer {
    Spectrum,
    C64,
    BBC
}

Computer my_computer = Computer::Spectrum;

switch(my_computer) {
    case(Computer::Spectrum) {
        // do speccy stuff
    } break;
    case(Computer::C64) {
        // do commodore stuff

    } break;
    case(Computer::BBC) {
        // do bbc micro stuff

    } break;
    default: {
        // do other stuff
    }
}

Plain Old Data classes (PODs)

struct Book {
    char name[256];
    int year;
    int number_of_pages;
}

Initialising a POD

struct Book {
    char name[256];
    int year;
    int number_of_pages;
}

Book hitch { "Hitchhiker's Guide", 1980, 42 };
Book empty { }; // all values set to zero
Book book1 { "Book with no pages", 2020 };  // pages set to zero

// can only omit fields from right to left, so this won't work...
Book bad_book { 1984, 275 };

Structs and Classes

The only difference between structs and classes is the default access modifier.

  • struct - default is PUBLIC
  • class - default is PRIVATE

Constructors

struct Book {
    Book() {
     //   ... constructor code ...
    }
};

Destructors

struct Something() {
    ~Something () {
        // clear up stuff here... e.g. file handles
    }
}
  • destructors must not take any arguments
  • almost never called explicitly