C++ header files

Thread Starter

Ryno3030

Joined Dec 1, 2007
9
Hello everybody. I am rather new to c++ programming, and I am having trouble with header files. I am specifically looking for resources on how to construct header files. The resources I have found just tell me what these files are used for. I am able to write the classes just fine. I'm just not sure the structure and syntax to use with headers. Any help would be great. Thanks in advance.
 

RiJoRI

Joined Aug 15, 2007
536
Header files are used at the "head" of the program to hide information such as declarations. I saw a C file once with over 400 lines of

#if this_model
void f1(void);
#else
int f1(void);
#endif

400 lines of usually-not-needed stuff before the first line of code! I immediately put it into a header file, so all that was left was

#include <thisFile.h>
/* start of code */

... and I coded happily ever after! :D

The other use of header files is to show the declarations of library functions. Take a look into stdio.h (or whatever C++ calls it) to see an example.

HTH,
--Rich
 

Thread Starter

Ryno3030

Joined Dec 1, 2007
9
I understand what you're saying, but I would like reference material on how to code header files. Can you simply put any definitions and declarations you need into a header file?
 

RiJoRI

Joined Aug 15, 2007
536
Yes -- you can put whatever you would like into header files, although putting code in the file is not a good practice. I use the following header format to keep like with like. I do not use all the classifications in all header files -- e.g., I may not have any external functions, so there's nothing there -- but I generally leave them in, because "code grows."

--Rich

Rich (BB code):
/***************************************************************************/
/********************************************************** include files **/

/***************************************************************************/
/****************************************************** local definitions **/

/***************************************************************************/
/***************************************************** external functions **/

/***************************************************************************/
/******************************************************* public functions **/

/***************************************************************************/
/***************************************************** internal functions **/

/***************************************************************************/
/********************************************************** external data **/

/***************************************************************************/
/************************************************************ public data **/

/***************************************************************************/
/*********************************************************** private data **/
 

bloguetronica

Joined Apr 27, 2007
1,541
Here is an example of a header file in a c library (RS232.h):
Rich (BB code):
/* RS232 Library - Version 1.0
   Copyright (c) 2007 Samuel Lourenço

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


   Please feel free to contact me via e-mail: samuel.fmlourenco@gmail.com */


#ifndef _H_RS232
#define _H_RS232

#ifdef _USE_RS232_DEFINES
#define RS_110BD   0  // Baud rates
#define RS_150BD   1
#define RS_300BD   2
#define RS_600BD   3
#define RS_1200BD  4
#define RS_2400BD  5
#define RS_4800BD  6
#define RS_9600BD  7
#define RS_5BIT    0  // Word size options
#define RS_6BIT    1
#define RS_7BIT    2
#define RS_8BIT    3
#define RS_NPARITY 0  // Parity options
#define RS_ODD     1
#define RS_EVEN    3
#define RS_SINGLE  0  // Single stop bit
#define RS_DOUBLE  1  // Double stop bit
#endif

short InitPort (short port, short baud, short size, short parity, short stops);
short Receive (short port);
short Status (short port);
short Transmit (short port, short byte);

#endif
And the respective file with the function implementations goes here (RS232.c):
Rich (BB code):
/* RS232 Library - Version 1.0
   Copyright (c) 2007 Samuel Lourenço

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


   Please feel free to contact me via e-mail:  */


#include <dos.h>
#include "RS232.h"

union REGS r;

// Returns port status in AH and modem status in AL
short InitPort (short port, short baud, short size, short parity, short stops)
{
    r.h.ah = 0x00;
    r.h.al = 32 * baud + 8 * parity + 4 * stops + size;
    r.x.dx = port;
    int86 (0x14, &r, &r);
    return r.x.ax;
}

// Returns port status in AH and the received character in AL
short Receive (short port)
{
    r.h.ah = 0x02;
    r.x.dx = port;
    int86 (0x14, &r, &r);
    return r.x.ax;
}

// Returns port status in AH and modem status in AL
short Status (short port)
{
    r.h.ah = 0x03;
    r.x.dx = port;
    int86 (0x14, &r, &r);
    return r.x.ax;
}

// Also returns port status in AH
short Transmit (short port, short byte)
{
    r.h.ah = 0x01;
    r.h.al = byte;
    r.x.dx = port;
    int86 (0x14, &r, &r);
    return r.h.ah;
}
C++ Is no diferent. Only template functions are implemented in header files. Normal functions are implemented in the respective CPP file. Here is another example (CSUtils.h):
Rich (BB code):
/* CSUtils Library - Version 2.0 - General purpose utility functions
   Copyright (c) 2006-2007 Samuel Lourenço

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


   Please feel free to contact me via e-mail: samuel.fmlourenco@gmail.com */


#ifndef _H_CSUtils
#define _H_CSUtils

namespace CS
{
    double DegToGrad (double angle);
    double DegToRad (double angle);
    unsigned short DwordHW (unsigned int dword);
    unsigned short DwordLW (unsigned int dword);
    short DwordHWHB (unsigned int dword);
    short DwordHWLB (unsigned int dword);
    short DwordLWHB (unsigned int dword);
    short DwordLWLB (unsigned int dword);
    double GradToDeg (double angle);
    double GradToRad (double angle);
    template<class T>
    T Max (T num1, T num2);
    template<class T>
    T Max (T num1, T num2, T num3);
    template<class T>
    T Max (T num1, T num2, T num3, T num4);
    template<class T>
    T Min (T num1, T num2);
    template<class T>
    T Min (T num1, T num2, T num3);
    template<class T>
    T Min (T num1, T num2, T num3, T num4);
    double RadToDeg (double angle);
    double RadToGrad (double angle);
    short WordHB (unsigned short word);
    short WordLB (unsigned short word);

    // Template functions implemented below
    template<class T>
    T Max (T num1, T num2)
    {
        return num1 > num2 ? num1 : num2;
    }

    template<class T>
    T Max (T num1, T num2, T num3)
    {
        return Max(Max (num1, num2), num3);
    }

    template<class T>
    T Max (T num1, T num2, T num3, T num4)
    {
        return Max(Max (num1, num2), Max (num3, num4));
    }

    template<class T>
    T Min (T num1, T num2)
    {
        return num1 < num2 ? num1 : num2;
    }

    template<class T>
    T Min (T num1, T num2, T num3)
    {
        return Min (Min (num1, num2), num3);
    }

    template<class T>
    T Min (T num1, T num2, T num3, T num4)
    {
        return Min(Min (num1, num2), Min (num3, num4));
    }
}

#endif
And the respective CPP file goes here:
Rich (BB code):
/* CSUtils Library - Version 2.0 - General purpose utility functions
   Copyright (c) 2006-2007 Samuel Lourenço

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


   Please feel free to contact me via e-mail: samuel.fmlourenco@gmail.com */


#define _USE_MATH_DEFINES

#include <cmath>
#include "CSUtils.h"

namespace CS
{
    double DegToGrad (double angle)
    {
        return 10 * angle / 9;
    }

    double DegToRad (double angle)
    {
        return M_PI * angle / 180;
    }

    unsigned short DwordHW (unsigned int dword)
    {
        return dword / 65536;
    }

    unsigned short DwordLW (unsigned int dword)
    {
        return dword % 65536;
    }

    short DwordHWHB (unsigned int dword)
    {
        return WordHB (DwordHW (dword));
    }

    short DwordHWLB (unsigned int dword)
    {
        return WordLB (DwordHW (dword));
    }

    short DwordLWHB (unsigned int dword)
    {
        return WordHB (DwordLW (dword));
    }

    short DwordLWLB (unsigned int dword)
    {
        return WordLB (DwordLW (dword));
    }

    double GradToDeg (double angle)
    {
        return 9 * angle / 10;
    }

    double GradToRad (double angle)
    {
        return M_PI * angle / 200;
    }

    double RadToDeg (double angle)
    {
        return 180 * angle / M_PI;
    }

    double RadToGrad (double angle)
    {
        return 200 * angle / M_PI;
    }

    short WordHB (unsigned short word)
    {
        return word / 256;
    }

    short WordLB (unsigned short word)
    {
        return word % 256;
    }
}
 
Top