Small Steps VI — Growing into C++

Understand the Program Entry Point

Understand the Program Entry Point

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Writing the real entry function main

What you will learn

main is the entry function of a C++ program. Until now, Small supplied main and had it call small_main. Now write setup and shutdown yourself.

Try it

Example · example1.cpp
#include <small.h>

int main()
{
    Small::initialize_small();

    Small::print("Hello from main!");

    Small::shutdown_small();
    return 0;
}

Writing your own main in this IDE also ends automatic header and namespace support. That is why you include small.h and use Small::.

Follow initialize_small → your work → shutdown_small. return 0 indicates normal termination. Check for Hello from main!.

Cleaning up the window before the runtime

What you will learn

A runtime provides the foundation for features such as windows and sound. Clean up objects using those features before shutting down their foundation.

Try it

Example · smallmain_2_example2.cpp
#include <small.h>

int main(int argc, char* argv[])
{
    Small::initialize_small(argc, argv);

    {
        Small::Window window;
        window.set_title("Real main()");
        window.open(500, 300);

        while (window.is_open())
        {
            window.clear(Small::Black);
            window.draw_text(30, 80, "This is ordinary C++ main()");
            window.show();
        }
    } // Destroy Window before shutting down the runtime.
    Small::shutdown_small();
    return 0;
}

When the inner brace scope ends, the Window object is cleaned up. Then call shutdown_small. Closing a window and ending an object's lifetime are different actions.

argc and argv receive information passed on the command line. This lesson is optional advanced material. You do not need to memorize all pointer notation yet.

Exercise

Change a small_main Hello program to a real main(). Write #include <small.h> yourself and use Small::initialize_small(), Small::print, and Small::shutdown_small().

Exercise starter · exercise1_starter.cpp
#include <small.h>

int main()
{
    // Initialize Small, print a message, shut Small down, and return 0.
}

Hint

Do more than rename the function: use return type int, call Small::shutdown_small(); before finishing, and then write return 0;.

show solution
#include <small.h>

int main()
{
    Small::initialize_small();

    Small::print("Hello from main!");

    Small::shutdown_small();
    return 0;
}
Try More (Optional)

Exercise

Write #include <small.h> and int main(int argc, char* argv[]). Pass argc and argv to Small::initialize_small, print argc, and call Small::shutdown_small() before the program finishes.

Exercise starter · smallmain_2_exercise2_starter.cpp
#include <small.h>

int main(int argc, char* argv[])
{
    // Initialize Small with argc and argv.
    // print argc, then shut Small down before returning.

    return 0;
}

Hint

After Small::initialize_small(argc, argv);, use Small::print("argc: ", argc);. Call Small::shutdown_small(); before return 0;.

show solution
#include <small.h>

int main(int argc, char* argv[])
{
    Small::initialize_small(argc, argv);

    Small::print("argc: ", argc);

    Small::shutdown_small();
    return 0;
}
← Previous lessonNext lesson →