1

I am a student programmer learning my first language with the book "Accelerated C++." I am at at a point where the author is explaining header files and source files containing functions. In the exercise the book provides for explanation, the author has header files that contain the definitions for functions, but it seems redundant as the source file also has the definition for the functions. What is the point of a header file in this case when programming C++?

Example: Header file.

#ifndef MEDIAN_H
#define MEDIAN_H

#include <vector>
double median(std::vector<double>);

#endif // MEDIAN_H

Then source file containing function to determine the median of grades:

// source file for the `median' function
#include <algorithm>    // to get the declaration of `sort'
#include <stdexcept>    // to get the declaration of `domain_error'
#include <vector>       // to get the declaration of `vector'

using std::domain_error;   using std::sort;   using std::vector;

#include "median.h"

// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec)
{
#ifdef _MSC_VER
    typedef std::vector<double>::size_type vec_sz;
#else
    typedef vector<double>::size_type vec_sz;
#endif

    vec_sz size = vec.size();
    if (size == 0)
        throw domain_error("median of an empty vector");

    sort(vec.begin(), vec.end());

    vec_sz mid = size/2;

    return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

The median.h is copied into the median function source file even though the source already has the definition vector<double> vec The book explains it as harmless and actually a good idea. But I just want to get a better understanding of the reason for such redundancy. Any explanation would be great!

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • The header typically has limited **declarations**. The implementation has full definitions. – chris Mar 04 '13 at 03:49
  • 4
    You'll see the need when you start working with projects with multiple files. – jman Mar 04 '13 at 03:50
  • http://stackoverflow.com/questions/1305947/why-does-c-need-a-separate-header-file – jman Mar 04 '13 at 03:52
  • @skjaidev thank you for the explanation and the post I should have looked a little better. The good thing is this post also touched on how the compiler links .cpp source files also which was another thought I had. – Brandon Feist Mar 04 '13 at 04:08

2 Answers2

2

The purpose of the header file is to be included by files besides the file which actually implements the function in question. Essentially, you need to tell the C++ compiler what functions exist, and what they look like in order to call them.

For example, let's say I have a function doStuff.cpp:

#include "median.h"

std::vector<double> my_vector;
double my_median;

void do_stuff(){
    my_median=median(my_vector);
}

The C++ compiler needs to know that the function median() has your specific arguments and return values as opposed to someone else's median().

Maz
  • 3,375
  • 1
  • 22
  • 27
  • This makes perfect sense, and is so simple I can't believe I couldn't grasp that concept. Thank you for the clear explanation. – Brandon Feist Mar 04 '13 at 04:04
0

Yes, this is redundant, but standard practice. The C/C++ compiler requires a declaration before the use of a function. In some cases, this means that you absolutely must have a declaration before an implementation, for example, if you have mutually recursive functions. If you have multiple files in a project, then you'll also need to include the declarations of functions that you're using from another file -- and the easiest way to do that is to have them separate.

That said, the declarations/implementations are also kept separate for reusability in libraries. Imagine you write a very large library of useful functions and someone else wants to use them. They need to provide the compiler with the declarations of the functions they will be using, so they use your .h file.

bchurchill
  • 1,410
  • 8
  • 23