Course of Raku / Objects, I/O, and exceptions / Classes and objects / Attributes / Exercises

A counter

Problem

Define a class Counter with a single attribute count that is changeable (is rw) and defaults to 0.

Create a counter and first print its count to confirm it starts at the default. Then increment it five times in a loop and print the final value.

Example

The program prints:

0
5

Solution

See the solution

Course navigation

Solution: A point class   |   Solution: A counter