C++ header files

Discussion in 'Programmer's Corner' started by Ryno3030, Dec 2, 2007.

  1. Ryno3030

    Thread Starter New Member

    Dec 1, 2007
    9
    0
    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.
     
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    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
     
  3. Ryno3030

    Thread Starter New Member

    Dec 1, 2007
    9
    0
    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?
     
  4. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    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

    Code ( (Unknown Language)):
    1.  
    2. /***************************************************************************/
    3. /********************************************************** include files **/
    4.  
    5. /***************************************************************************/
    6. /****************************************************** local definitions **/
    7.  
    8. /***************************************************************************/
    9. /***************************************************** external functions **/
    10.  
    11. /***************************************************************************/
    12. /******************************************************* public functions **/
    13.  
    14. /***************************************************************************/
    15. /***************************************************** internal functions **/
    16.  
    17. /***************************************************************************/
    18. /********************************************************** external data **/
    19.  
    20. /***************************************************************************/
    21. /************************************************************ public data **/
    22.  
    23. /***************************************************************************/
    24. /*********************************************************** private data **/
    25.  
     
  5. Ryno3030

    Thread Starter New Member

    Dec 1, 2007
    9
    0
    Thanks for your help! :)
     
  6. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    Here is an example of a header file in a c library (RS232.h):
    Code ( (Unknown Language)):
    1. /* RS232 Library - Version 1.0
    2.    Copyright (c) 2007 Samuel Lourenço
    3.  
    4.    This library is free software; you can redistribute it and/or
    5.    modify it under the terms of the GNU Lesser General Public
    6.    License as published by the Free Software Foundation; either
    7.    version 2.1 of the License, or (at your option) any later version.
    8.  
    9.    This library is distributed in the hope that it will be useful,
    10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
    11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12.    Lesser General Public License for more details.
    13.  
    14.    You should have received a copy of the GNU Lesser General Public
    15.    License along with this library; if not, write to the Free Software
    16.    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17.  
    18.  
    19.    Please feel free to contact me via e-mail: samuel.fmlourenco@gmail.com */
    20.  
    21.  
    22. #ifndef _H_RS232
    23. #define _H_RS232
    24.  
    25. #ifdef _USE_RS232_DEFINES
    26. #define RS_110BD   0  // Baud rates
    27. #define RS_150BD   1
    28. #define RS_300BD   2
    29. #define RS_600BD   3
    30. #define RS_1200BD  4
    31. #define RS_2400BD  5
    32. #define RS_4800BD  6
    33. #define RS_9600BD  7
    34. #define RS_5BIT    0  // Word size options
    35. #define RS_6BIT    1
    36. #define RS_7BIT    2
    37. #define RS_8BIT    3
    38. #define RS_NPARITY 0  // Parity options
    39. #define RS_ODD     1
    40. #define RS_EVEN    3
    41. #define RS_SINGLE  0  // Single stop bit
    42. #define RS_DOUBLE  1  // Double stop bit
    43. #endif
    44.  
    45. short InitPort (short port, short baud, short size, short parity, short stops);
    46. short Receive (short port);
    47. short Status (short port);
    48. short Transmit (short port, short byte);
    49.  
    50. #endif
    51.  
    And the respective file with the function implementations goes here (RS232.c):
    Code ( (Unknown Language)):
    1. /* RS232 Library - Version 1.0
    2.    Copyright (c) 2007 Samuel Lourenço
    3.  
    4.    This library is free software; you can redistribute it and/or
    5.    modify it under the terms of the GNU Lesser General Public
    6.    License as published by the Free Software Foundation; either
    7.    version 2.1 of the License, or (at your option) any later version.
    8.  
    9.    This library is distributed in the hope that it will be useful,
    10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
    11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12.    Lesser General Public License for more details.
    13.  
    14.    You should have received a copy of the GNU Lesser General Public
    15.    License along with this library; if not, write to the Free Software
    16.    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17.  
    18.  
    19.    Please feel free to contact me via e-mail:  */
    20.  
    21.  
    22. #include <dos.h>
    23. #include "RS232.h"
    24.  
    25. union REGS r;
    26.  
    27. // Returns port status in AH and modem status in AL
    28. short InitPort (short port, short baud, short size, short parity, short stops)
    29. {
    30.     r.h.ah = 0x00;
    31.     r.h.al = 32 * baud + 8 * parity + 4 * stops + size;
    32.     r.x.dx = port;
    33.     int86 (0x14, &r, &r);
    34.     return r.x.ax;
    35. }
    36.  
    37. // Returns port status in AH and the received character in AL
    38. short Receive (short port)
    39. {
    40.     r.h.ah = 0x02;
    41.     r.x.dx = port;
    42.     int86 (0x14, &r, &r);
    43.     return r.x.ax;
    44. }
    45.  
    46. // Returns port status in AH and modem status in AL
    47. short Status (short port)
    48. {
    49.     r.h.ah = 0x03;
    50.     r.x.dx = port;
    51.     int86 (0x14, &r, &r);
    52.     return r.x.ax;
    53. }
    54.  
    55. // Also returns port status in AH
    56. short Transmit (short port, short byte)
    57. {
    58.     r.h.ah = 0x01;
    59.     r.h.al = byte;
    60.     r.x.dx = port;
    61.     int86 (0x14, &r, &r);
    62.     return r.h.ah;
    63. }
    64.  
    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):
    Code ( (Unknown Language)):
    1. /* CSUtils Library - Version 2.0 - General purpose utility functions
    2.    Copyright (c) 2006-2007 Samuel Lourenço
    3.  
    4.    This library is free software; you can redistribute it and/or
    5.    modify it under the terms of the GNU Lesser General Public
    6.    License as published by the Free Software Foundation; either
    7.    version 2.1 of the License, or (at your option) any later version.
    8.  
    9.    This library is distributed in the hope that it will be useful,
    10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
    11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12.    Lesser General Public License for more details.
    13.  
    14.    You should have received a copy of the GNU Lesser General Public
    15.    License along with this library; if not, write to the Free Software
    16.    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17.  
    18.  
    19.    Please feel free to contact me via e-mail: samuel.fmlourenco@gmail.com */
    20.  
    21.  
    22. #ifndef _H_CSUtils
    23. #define _H_CSUtils
    24.  
    25. namespace CS
    26. {
    27.     double DegToGrad (double angle);
    28.     double DegToRad (double angle);
    29.     unsigned short DwordHW (unsigned int dword);
    30.     unsigned short DwordLW (unsigned int dword);
    31.     short DwordHWHB (unsigned int dword);
    32.     short DwordHWLB (unsigned int dword);
    33.     short DwordLWHB (unsigned int dword);
    34.     short DwordLWLB (unsigned int dword);
    35.     double GradToDeg (double angle);
    36.     double GradToRad (double angle);
    37.     template<class T>
    38.     T Max (T num1, T num2);
    39.     template<class T>
    40.     T Max (T num1, T num2, T num3);
    41.     template<class T>
    42.     T Max (T num1, T num2, T num3, T num4);
    43.     template<class T>
    44.     T Min (T num1, T num2);
    45.     template<class T>
    46.     T Min (T num1, T num2, T num3);
    47.     template<class T>
    48.     T Min (T num1, T num2, T num3, T num4);
    49.     double RadToDeg (double angle);
    50.     double RadToGrad (double angle);
    51.     short WordHB (unsigned short word);
    52.     short WordLB (unsigned short word);
    53.  
    54.     // Template functions implemented below
    55.     template<class T>
    56.     T Max (T num1, T num2)
    57.     {
    58.         return num1 > num2 ? num1 : num2;
    59.     }
    60.  
    61.     template<class T>
    62.     T Max (T num1, T num2, T num3)
    63.     {
    64.         return Max(Max (num1, num2), num3);
    65.     }
    66.  
    67.     template<class T>
    68.     T Max (T num1, T num2, T num3, T num4)
    69.     {
    70.         return Max(Max (num1, num2), Max (num3, num4));
    71.     }
    72.  
    73.     template<class T>
    74.     T Min (T num1, T num2)
    75.     {
    76.         return num1 < num2 ? num1 : num2;
    77.     }
    78.  
    79.     template<class T>
    80.     T Min (T num1, T num2, T num3)
    81.     {
    82.         return Min (Min (num1, num2), num3);
    83.     }
    84.  
    85.     template<class T>
    86.     T Min (T num1, T num2, T num3, T num4)
    87.     {
    88.         return Min(Min (num1, num2), Min (num3, num4));
    89.     }
    90. }
    91.  
    92. #endif
    93.  
    And the respective CPP file goes here:
    Code ( (Unknown Language)):
    1. /* CSUtils Library - Version 2.0 - General purpose utility functions
    2.    Copyright (c) 2006-2007 Samuel Lourenço
    3.  
    4.    This library is free software; you can redistribute it and/or
    5.    modify it under the terms of the GNU Lesser General Public
    6.    License as published by the Free Software Foundation; either
    7.    version 2.1 of the License, or (at your option) any later version.
    8.  
    9.    This library is distributed in the hope that it will be useful,
    10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
    11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12.    Lesser General Public License for more details.
    13.  
    14.    You should have received a copy of the GNU Lesser General Public
    15.    License along with this library; if not, write to the Free Software
    16.    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17.  
    18.  
    19.    Please feel free to contact me via e-mail: samuel.fmlourenco@gmail.com */
    20.  
    21.  
    22. #define _USE_MATH_DEFINES
    23.  
    24. #include <cmath>
    25. #include "CSUtils.h"
    26.  
    27. namespace CS
    28. {
    29.     double DegToGrad (double angle)
    30.     {
    31.         return 10 * angle / 9;
    32.     }
    33.  
    34.     double DegToRad (double angle)
    35.     {
    36.         return M_PI * angle / 180;
    37.     }
    38.  
    39.     unsigned short DwordHW (unsigned int dword)
    40.     {
    41.         return dword / 65536;
    42.     }
    43.  
    44.     unsigned short DwordLW (unsigned int dword)
    45.     {
    46.         return dword % 65536;
    47.     }
    48.  
    49.     short DwordHWHB (unsigned int dword)
    50.     {
    51.         return WordHB (DwordHW (dword));
    52.     }
    53.  
    54.     short DwordHWLB (unsigned int dword)
    55.     {
    56.         return WordLB (DwordHW (dword));
    57.     }
    58.  
    59.     short DwordLWHB (unsigned int dword)
    60.     {
    61.         return WordHB (DwordLW (dword));
    62.     }
    63.  
    64.     short DwordLWLB (unsigned int dword)
    65.     {
    66.         return WordLB (DwordLW (dword));
    67.     }
    68.  
    69.     double GradToDeg (double angle)
    70.     {
    71.         return 9 * angle / 10;
    72.     }
    73.  
    74.     double GradToRad (double angle)
    75.     {
    76.         return M_PI * angle / 200;
    77.     }
    78.  
    79.     double RadToDeg (double angle)
    80.     {
    81.         return 180 * angle / M_PI;
    82.     }
    83.  
    84.     double RadToGrad (double angle)
    85.     {
    86.         return 200 * angle / M_PI;
    87.     }
    88.  
    89.     short WordHB (unsigned short word)
    90.     {
    91.         return word / 256;
    92.     }
    93.  
    94.     short WordLB (unsigned short word)
    95.     {
    96.         return word % 256;
    97.     }
    98. }
    99.  
     
Loading...