Small Steps VI — Growing into C++

Writing the real entry function main

Write 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!.

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;
}
← Previous lessonNext lesson →