Small Steps II — Making Things

Asking an object for information

Ask an object for information.

What you will learn

Writing a function name after an object's dot uses an operation belonging to that object. Such a function is a member function.

Try it

Example · example2.cpp
void small_main()
{
    Window window;

    window.set_title("Small Window");
    window.open(400, 300);

    print("Size: ", window.width(), " x ", window.height());

    while (window.is_open())
    {
        window.show();
    }
}

window.width() and window.height() return the window's width and height. Check that the console shows Size: 400 x 300.

Creating the object and opening the actual window are separate actions. You can call set_title before open. Call close to close it from your program.

Exercise

Open a 320 × 240 window, print its actual width and height in the console, and keep the window open.

Exercise starter · exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(320, 240);

    // print the size, then keep the window alive.
}

Hint

Pass window.width() and window.height() to print.

show solution
void small_main()
{
    Window window;
    window.open(320, 240);

    print(window.width(), " x ", window.height());

    while (window.is_open())
        window.show();
}
← Previous lessonNext lesson →