C++ Programming Series: Headers and Prototypes (Part 2, Last)

In this post, we are going to discuss some confusions that you might have got in the previous post. You might have read #include "func.h" in that post and not #include <func.h>. Actually, both have different meanings!

If we type angle brackets (<>) instead of double quotes (“”) for func.h, the compiler will check for that file in the C++ base files directory and project specified include directory. From C++ base files, I mean those files which also includes the most commonly used iostream, math.h, string, etc, that are the files of standard C++. All these C++ standard files are header files although they may not have .h at the end of their names.

If we type double quotes as in the previous post, the compiler will check for that file in local project directory. This directory is the same directory where the IDE adds all our files to.

In a nutshell,

  • #include <file_name> – searches the file in base and project specified include directory.
  • #include “file_name” – searches the file in local project directory.

Now, we know what double quotes and angle brackets do with #include. But we actually, don’t know what #include do! #include simply, copy the codes from the file and then, paste it into that file where it is called.

This is something that we don’t need to know. What we need to know is the fact that C++ don’t allow more than one definitions. You can’t just do like that:

void func()
{
  //code here...
}

//Another time: gives error!
void func()
{
  //code here...
}

It might be possible that we include things twice or, the other and most common case is that the file that we are including, includes another file that is already included in our file! To make it clear, see the code below:

#include "a" //'a' file contains function 'func()'
#include "b" //'b' file includes file 'a'
//'func()' is defined twice
//So, gives error!

That hash(#) before the include suggests that the command after it, is a preprocessor directive and not a C++ code. These directives runs before the compilation of the actual program. There are many of these. You can check out preprocessor directives in details here.

In order to make sure that something is defined just once, you can use preprocessor directives like that:

#ifndef SOMETHING //This means 'if not defined preprocessor term called SOMETHING'
#define
  //code here
#endif //This ends the '#ifndef' statement

The above code just acts similar to the C++ if-statement that we discussed before.

SOMETHING can be anything. It can be an integer, a group of characters, a float, etc.

#define PI 3.14 //Now, PI is equal to 3.14

int main()
{
  cout << PI << endl; //We can use it as a regular double
  PI++; //Gives error; preprocessor definations are constants!
  return 0;
}

I hope that you got all of the above things. Just practice using headers and prototypes. Find errors and then, try to fix them yourself. I got a new exciting series called My Pixels where I have showcased some of my hand drawn pixel arts or sprites! Just check that out and see ya!

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 a 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 as 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 as 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 approriate 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

//Definations
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 make 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.