Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Headers and Prototypes (Part 1)

C++ Programming Series: Headers and Prototypes (Part 1)

Until now, we have been working with codes which are present in just a single file. We have been doing single file projects till now, in which all the codes are being typed in one file. This approach is good for tiny projects. However, when we are going to have some medium-sized project to be worked on, this approach is really horrible! Imagine a file with ten thousand lines of codes. Of course, we did all 10000 lines of codes in the form of functions but still, the infrastructure of our program is ugly and hardly manageable now. It is less likely to be modified by some other programmer or even you. Maybe, you can do some modifications but when you leave your program for a month and then, came back to it, you would like not to read the code because it looks to be too big and lengthy!

The solution for bigger projects is to have multiple files in a project and this approach is called a multiple-file project. In this approach, there are three kinds of files: the one with extension ‘.cpp’ which contains the main() function, the other with extension ‘.h’ or ‘.hpp’ which contains all the possible declarations of a group of related functions or entities, and the last one with extension ‘.cpp’ which contains all the definitions of the declarations. Creating, importing and exporting static and dynamic libraries is another topic which we will not discuss in this series.

The files with extension ‘.h’ or ‘.hpp’, are called Header files which contains the declarations only and not the definitions. Now, the advantage of the header file is to help the programmer to know all the details of the functions that he/she want to use in his/her program without even skipping through all the enormous definitions!

The declaration of a function is often, referred to as Prototypes.

Example of a declaration of an integer:

//Declaration
extern int value0;

//'extern' is a keyword used to write declarations of variables

//Declaration and Definition
int value1;

//Declaration along with Definition and Initialization
int value2 = 15;

Example of a declaration of a function/Prototype:

//Declaration only/Prototype
int square(int value);

//Declaration along with Defination
int square(int value)
{
    return (value*value);
}

In Visual Studio, in order to have multiple file project, just create new files of appropriate file extensions. Suppose that we have three files namely, main.cpp, func.h and func.cpp.

Here the codes are shown for both single and multiple file approaches using Prototypes:

Single File Project:
In main.cpp:

//Includings
#include <iostream>

//Prototypes
int square(int value);
int enterAnInt();
void printAnInt(int value);

//Main Function
int main()
{
    //Prints the square of the value given by the user
    printAnInt(square(enterAnInt()));
    return 0;
}

//Definations
int square(int value)
{
    return (value*value);
}
int enterAnInt()
{
    int value;
    cin >> value;
    return value;
}
void printAnInt(int value)
{
    cout << value << endl;
}

Multiple File Project:
In func.h:

//Includings on which the functions depends on
#include <iostream>

//Prototypes
int square(int value);
int enterAnInt();
void printAnInt(int value);

In main.cpp:

//Main Function
#include "func.h" //Including header to access all functions

int main()
{
    //Prints the square of the value given by the user
    printAnInt(square(enterAnInt()));
    return 0;
}

In func.cpp:

#include "func.h" //Including header to access all functions

//Definitions
int square(int value)
{
    return (value*value);
}
int enterAnInt()
{
    int value;
    cin >> value;
    return value;
}
void printAnInt(int value)
{
    cout << value << endl;
}

That’s the basics of having multiple file projects! This approach is also a modern approach for projects bigger than tiny! Also, I will use this approach for game development and will avoid the single file approach because it increases the quality of our code and makes it more readable.

Your only task is to make a simplistic multiple file project like the one above without errors. Just try to handle the errors and find there fixes on the internet. You can ask me here as well if you encounter some errors.

2 responses to “C++ Programming Series: Headers and Prototypes (Part 1)”

  1. You said “int value0;” is a declaration only but it’s actually a definition. If you try to declare the variable like this twice you’ll notice that it doesn’t work. To declare a global variable you would have to use the extern keyword.

    // file.h
    extern int value0;

    // file.cpp
    int value0 = 15;

Leave a Reply

Your email address will not be published. Required fields are marked *