🚀 Alguni Start learning

Unit 5: Structs & Enums

Grouping things that belong together.

Unit 5 of 8 in C++ for kids. Its 4 lessons are Grouping Values, Structs That Do Things, Named Numbers and Struct Master — below is everything each one explains, and a question or two from it to try.

Every sample on this page was compiled and run with g++ before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

📦 Grouping Values

One name for several values

A struct groups related values into a single thing. In C++ the name alone is the type — no extra word needed when you use it.

C++

#include <iostream>
using namespace std;

struct Point {
    int x;
    int y;
};

int main() {
    Point p;
    p.x = 3;
    p.y = 4;
    cout << p.x << " " << p.y << endl;
    return 0;
}

It prints

3 4

Members can be any type

Including a string, which C++ makes far easier than C ever did.

C++

#include <iostream>
#include <string>
using namespace std;

struct Pet {
    string name;
    int age;
};

int main() {
    Pet cat;
    cat.name = "Tig";
    cat.age = 3;
    cout << cat.name << " is " << cat.age << endl;
    return 0;
}

It prints

Tig is 3

A whole struct can be copied

Assigning one copies every member, so the two are then completely separate.

C++

#include <iostream>
using namespace std;

struct Point { int x; int y; };

int main() {
    Point a;
    a.x = 1;
    a.y = 2;
    Point b = a;
    b.x = 9;
    cout << a.x << " " << b.x << endl;
    return 0;
}

It prints

1 9

Try it yourself

Why does the struct definition end with a semicolon?

  • A mistake in the language
  • It is a declaration, so it finishes like any other
  • To separate it from main
  • It does not need one

What does this print?

C++

#include <iostream>
using namespace std;

struct Box { int w; int h; };

int main() {
    Box b;
    b.w = 3;
    b.h = 5;
    cout << b.w * b.h << endl;
    return 0;
}

Answer them in the app

⚙️ Structs That Do Things

A function inside the struct

This is where C++ leaves C behind. A struct can hold functions as well as values, and inside one the members are simply there by name.

C++

#include <iostream>
using namespace std;

struct Point {
    int x;
    int y;

    int sum() {
        return x + y;
    }
};

int main() {
    Point p;
    p.x = 3;
    p.y = 4;
    cout << p.sum() << endl;
    return 0;
}

It prints

7

A method can change its own members

The change lasts, because the method is working on that very object.

C++

#include <iostream>
using namespace std;

struct Counter {
    int count;

    void bump() {
        count = count + 1;
    }
};

int main() {
    Counter c;
    c.count = 0;
    c.bump();
    c.bump();
    cout << c.count << endl;
    return 0;
}

It prints

2

Methods make the struct explain itself

A method puts the rule next to the data it works on, so nobody has to remember how to work an area out.

C++

#include <iostream>
using namespace std;

struct Rect {
    int width;
    int height;

    int area() { return width * height; }
    bool isSquare() { return width == height; }
};

int main() {
    Rect r;
    r.width = 4;
    r.height = 4;
    cout << r.area() << endl;
    cout << boolalpha << r.isSquare() << endl;
    return 0;
}

It prints

16
true

Try it yourself

Inside a method, why can you write x instead of p.x?

  • x is a global
  • A method already belongs to one object, so its members are in reach by name
  • C++ guesses
  • You cannot

Answer it in the app

🏷️ Named Numbers

enum gives numbers names

An enum is a list of named whole numbers, counting from 0. A name explains far more than a bare 2 ever will.

C++

#include <iostream>
using namespace std;

enum Colour { RED, GREEN, BLUE };

int main() {
    Colour c = GREEN;
    cout << RED << " " << GREEN << " " << BLUE << endl;
    cout << c << endl;
    return 0;
}

It prints

0 1 2
1

You can choose the numbers

Give one a value and the rest carry on from there.

C++

#include <iostream>
using namespace std;

enum Level { LOW = 5, MEDIUM, HIGH = 10 };

int main() {
    cout << LOW << " " << MEDIUM << " " << HIGH << endl;
    return 0;
}

It prints

5 6 10

switch reads well with an enum

Checking one value against several possibilities is exactly what switch is for. Each case needs a break, or C++ carries on into the next one.

C++

#include <iostream>
using namespace std;

enum Colour { RED, GREEN, BLUE };

int main() {
    Colour light = GREEN;
    switch (light) {
        case RED:
            cout << "Stop" << endl;
            break;
        case GREEN:
            cout << "Go" << endl;
            break;
        default:
            cout << "Wait" << endl;
    }
    return 0;
}

It prints

Go

Try it yourself

What happens when a case has no break?

  • Nothing, break is optional
  • C++ carries straight on into the next case
  • The program stops
  • It repeats the case

What does this print?

C++

#include <iostream>
using namespace std;

int main() {
    int n = 2;
    switch (n) {
        case 1:
            cout << "a" << endl;
        case 2:
            cout << "b" << endl;
        case 3:
            cout << "c" << endl;
            break;
        default:
            cout << "d" << endl;
    }
    return 0;
}

Answer them in the app

🏆 Struct Master

Try it yourself

What does this print?

C++

#include <iostream>
using namespace std;

struct P { int x; };

int main() {
    P a;
    a.x = 1;
    P b = a;
    a.x = 100;
    cout << a.x << " " << b.x << endl;
    return 0;
}

Answer it in the app