Tie koodariksi

C++ Programming

Chapter 12: Records and Classes

Record

A record is a variable that can contain many named values, called fields. The record type type is created using struct, after which it can be used in the code.

For example, in the following record point there are two int fields x and y: x and y coordinates of the point. The code creates a point (1,3) and then changes the x coordinate.

struct point {
    int x, y;
};

int main() {
    point a = {1,3};
    cout << a.x << " " << a.y << "\n"; // 1 3
    a.x = 2;
    cout << a.x << " " << a.y << "\n"; // 2 3
}

Processing a record

The record behaves the same as when copying an ordinary variable. For example, in the following code, the content of a will be copied to b. Changing b later will not affect a.
point a = {1,3};
point b = a;
b.x = 2;
cout << a.x << " " << a.y << "\n"; // 1 3
cout << b.x << " " << b.y << "\n"; // 2 3
In the following code, p is a pointer to a, and can be used to make changes to a. The -> entry refers to a record field through the pointer, and p->x has the same meaning as (*p).x.
point a = {1,3};
point *p = &a;
p->x = 2;
cout << a.x << " " << a.y << "\n"; // 2 3
cout << p->x << " " << p->y << "\n"; // 2 3

Classes and objects

In addition to fields, a record may include functions, which allow you to manipulate fields. In this case, a record is called a class and the record variable is called an object.

For example, the following code defines a class counter with a member field x, the counter's value, and two functions: increment, which increases the counter's value by one and value, which returns the value.

struct counter {
    int x = 0;

    void increment() {
        x++;
    }

    int value() {
        return x;
    }
};

int main() {
    counter a;
    cout << a.value() << "\n"; // 0
    a.increment();
    cout << a.value() << "\n"; // 1
    a.increment();
    cout << a.value() << "\n"; // 2
}
The idea of the class is to bring together into one entity related information and the functions that handle it. This is the starting point for object-oriented programming.

Constructor

A constructor is a special function that is executed when the object is created. The constructor name is the same as the class name and has no type. For example, the following constructor prints a line text:
struct test {
    test() {
        cout << "Hello!\n";
    }
};

int main() {
    test t; // prints "Hello!"
}
A common use of a constructor is to initialize an object when creating it. A class can have several constructors, which may have different parameters.

For example, the following class has two constructors. The first constructor has no parameter and set the initial value to 0. In the second constructor, the initial value is given as a parameter.

struct counter {
    int x;

    counter() {
        x = 0;
    }

    counter(int a) {
        x = a;
    }

    // ...
};
The following code illustrates:
counter a;
counter b(5);
cout << a.value() << " " << b.value() << "\n"; // 0 5
Here's an even shorter way to do the same thing using the initializer list:
struct counter {
    int x;

    counter() : x(0) {}

    counter(int a) : x(a) {}

    // ...
};

Visibility of class components

In the next class, the x field is private, which means it can be used only within the class. On the other hand, the functions are public, so they can be used outside the class (as well as inside it).
struct counter {
private:
    int x = 0;

public:
    void increment() {
        x++;
    }

    int value() {
        return x;
    }
};
Because the functions are public, we can use them as before:
int main() {
    counter a;
    a.increment();
    cout << a.value() << "\n";
}
However, we can no longer access the x field directly:
int main() {
    counter a;
    a.x++; // error!
    cout << a.x << "\n"; // error!
}
Here the idea is that the class's public functions provide the interface that we use to access the object, and we do not have access to the internal-class implementation.

Datatähti 2027 – peruskoulun ja lukion ohjelmointikilpailu käynnissä 26.10.–8.11.2026