C++ Header Files Tutorial


This is a brief tutorial/overview on header files used in C++. In C++, you can declare a method or class using forward prototype before int main() [like so: int function(int x);] or you can declare them using a header. A header file is essentially a file that holds declarations of methods/functions, other .cpp files, classes, and other header files. Header files end in .h and its a way to organise code and other files, libraries, and packages of a C++ program that you would use. When declarations are done in a header file, it makes using the implementation in the main classes of the program more manageable.

For example, if you wanted to include a header file that has declarations already, simply add the line of code: #include “declarations.h”

The sample code below displays how methods, classes, and other header files are declared in a header file; as well as including a header file into the main .cpp file.
Header file:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once
//header files declared below
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

// reference additional headers your program requires here
void dostuff();//a method
int factorial(int n);//another method
class AObject; //a class declaration
class Calc//another type of class declaration
{
public:
	Calc(){}
	void Add(int a, int b);
};

Main.cpp

#include "stdafx.h"
#include <iostream>
#include "stdio.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"C++ factorial: ";
	int f;
	cin >> f;
	cout<<factorial(f)<<endl;
	dostuff();

	system("pause");
	return 0;
}
void dostuff()
{
	cout<<"dostuff"<<endl;
	return;
}
int factorial(int n)
{
	if(n==0)
	{
		return 1;
	}
	int ans = factorial(n-1)*n;
	return ans;
}
void Calc::Add(int a, int b)//here we implement the Add method delcared in Calc class in stdafx.h
{
	int sum = a+b;
	return;
}

From learncpp.com:
Here are a few best practices for creating your own header files.

  • Always include header guards.
  • Do not declare variables in header files unless they are constants. Header files should generally only be used for declarations.
  • Do not define functions in header files unless they are trivial. Doing so makes your header files harder for humans to read.
  • Each header file should have a specific job, and be as independent as possible. For example, you might put all your declarations related to functionality A in A.h and all your declarations related to functionality B in B.h. That way if you only care about A later, you can just include A.h and not get any of the stuff related to B.
  • Try to include as few other header files as possible in your header files.

Diagram of the linking of header files to main.cpp and compiling process

Advertisements

2 thoughts on “C++ Header Files Tutorial

  1. Pingback: C++ Templates Demo | Bits and Pieces of Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s