What should be in my header file in C ++?

I am just starting my first C ++ program, and I am almost just learning when I go. It is assumed that the program should have 3 different files, a header file (prog1.h), a prog1.cpp file (not sure about the correct terminology for this) and a test file that includes: for testing our program (prog1_test.cpp) .

Not asking for help in any of them (nevertheless, I’m sure that I will send one more question as soon as I get in it), but I thought that you need to know what the program should do in order to understand my question. Our program should read in some numbers from a file and put these numbers in a 2D array. Then it must assign symbols to each value of the number and print the current array and the "image" created using the symbols. Then it will go through the array, making sure that each number does not differ in value from its neighbors by more than 1, if so, the program should replace this number with the average value of its neighboring values. The program then prints this adjusted array and the "image" created with the characters assigned to the adjusted array.

I know that header files should include code that will be used in several files, but it’s hard for me to understand which parts of my program I need to go into this file. For example, will my code be included to open, read and close the data file or not, since the file only opens, reads and closes once?

+4
source share
3 answers

Header files contain declarations of functions and classes. They simply declare the name of the function, its return type, and a list of arguments. The .cpp file contains definitions of these functions — that is, the actual implementation. The header file is displayed in the rest of the program if you are #includein other files, but implementation details are hidden in the .cpp file.

, / .cpp , .h, , , .. .cpp . foo() .

:

prog1.h:

#include <iostream> // and whatever other libraries you need to include

#define ARRAY_SIZE 100 // and other defines

// Function declarations

// Read number from file, return int
void read_number(int array[ARRAY_SIZE][ARRAY_SIZE]);

char assign_char(int n);

void print_array(int array[ARRAY_SIZE][ARRAY_SIZE]);

void neighbor_check(int array[ARRAY_SIZE][ARRAY_SIZE]);

prog1.cpp:

// included headers, defines, and functions declared in prog1.h are visible
#include "prog1.h"

void read_number(int array[ARRAY_SIZE][ARRAY_SIZE]) {
    // implementation here
}

char assign_char(int n) {
    char c;
    // implementation here
    return c;

}

void print_array(int array[ARRAY_SIZE][ARRAY_SIZE]) {
    // implementation here
}

void neighbor_check(int array[ARRAY_SIZE][ARRAY_SIZE]) {
    // implementation here
}

// not visible to anything that #includes prog1.h
// since it is not declared in prog1.h
void foo() {
    // implementation here
}

prog1_test.cpp:

// included headers, defines, and functions declared in prog1.h are visible
#include "prog1.h"
// any other includes needed by main()

int main() {
   int array[ARRAY_SIZE][ARRAY_SIZE];

   read_number(array);

   for (int i = 0; i < ARRAY_SIZE; i++) {
       for (int j = 0; j < ARRAY_SIZE; j++) {
           assign_char(array[i][j]);
       }
   }

   neighbor_check(array);

   print_array(array);

   return 0;
}
+6

/ . , , , .

, , , , . , , , arrays.c. , , , , . , 5 , , arrays.c.

, , . , read_array 5 , read_array arrays.c, , .

header? , , . arrays.h , . , arrays.h arrays.c? header files, function declarations, data structures and variables , function definitions, arrays.c. , , , #include "arrays.h" , , arrays.c

( C) . , , read_array, prn_array, , free_array. array.c. ( , array.h .) , :

#include "array.h"

char **read_array (char *filename)
{
    char *buffer = NULL;
    size_t len = 0;
    ssize_t read;
    char **array = NULL;
    int cnt = 0;

    FILE *fp;
    fp = fopen (filename, "r");      //open file , read only
    if (!fp) {
        fprintf (stderr, "failed to open file for reading\n");
        return NULL;
    }

    array = calloc (AMAX, sizeof (*array) * AMAX); /* allocate pointers, set NULL */

    while ((read = getline (&buffer, &len, fp)) != -1) { /* read each line */

        if (cnt == AMAX) {
            fprintf (stderr, "Error: AMAX reached\n");
            break;
            /* you will realloc *array here in real life */
        }

        if (buffer[read-1] == '\n') {   /* strip newline              */
            buffer[read-1] = 0;
            read -= 1;
        }

        array[cnt] = strdup (buffer);   /* copy buffer to array[cnt]   */
        cnt++;                          /* increment counter           */
    }

    fclose (fp);                        /* close file stream (fp)      */
    return array;
}

void prn_array (char **array)
{
    register int j = 0;
    while (array[j]) {
        printf ("  array[%d]: %s\n", j, array[j]);
        j++;
    }
}

void free_array (char **array)
{
    register int j = 0;
    while (array[j]) {      /* for each allocated string */
        free (array[j]);    /* free pointer to string    */    
        j++;
    }
    free (array);           /* free array                */
}

. function declarations variables data structures, system headers included header files, array.h:

#ifndef MY_ARRAY_H
#define MY_ARRAY_H  1

/* headers required for array.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define AMAX 100

/* function declaration */
char **read_array (char *filename);
void prn_array (char **array);
void free_array (char **array);

#endif  /* MY_ARRAY_H */

#ifndef MY_ARRAY_H, , MY_ARRAY_H , ifndef ( ), , , #define, if #endif, MY_ARRAY_H, - , . ( , MY_ARRAY_H 1)

, array.h arrays.c ? , arrays.c arrays.c , array.h . ( application.c ):

#include <stdio.h>
#include "array.h"                      /* include array.h  */

int main (int argc, char *argv[]) {

    if (argc < 2) {
        printf ("filename.csv please...\n");
        return 1;
    }

    char **my_array = NULL;             /* declare pointers */

    my_array = read_array (argv[1]);    /* call read_array  */
    prn_array (my_array);               /* call prn_array   */
    free_array (my_array);              /* call free_array  */

    return 0;
}

application.c array.c:

gcc -Wall -Wextra -o application application.c array.c

( 100 )

$ ./application input.txt
  array[0]: 1.  This is a simple input file with line numbers
  array[1]: 2.  for use with application showing the use of
  array[2]: 3.  header file: array.h
  array[3]: 4.  array functions defined in: array.c
  array[4]: 5.  (blank)
  array[5]: 6.  compiled with the following:
  array[6]: 7.  gcc -Wall -Wextra -o application application.c array.c
  array[7]: 8.  --------
  array[8]: 9.  Now you know!

, , , , . , , arrays.h. , arrays.c , .

, , , . C/++.

+3

.

  • , , .
  • . : .
  • .
  • define
  • . .c .
  • ifndef
  • .
  • doxygen .
  • doxygen. ....

.

+1

All Articles