开发者

Understanding Header files

开发者 https://www.devze.com 2023-02-25 20:34 出处:网络
I\'m sort of confused between header files and functions now. Can someone tell me of a scenario where I could use 4 header files with 4 different calculations for functions? (It can be simple ones; I

I'm sort of confused between header files and functions now.

Can someone tell me of a scenario where I could use 4 header files with 4 different calculations for functions? (It can be simple ones; I've only started C++ last semester.) And the professor has confused me so I was hoping for some assistance here.

I understand you define a function within the header file. But I don't understand how to use a heade开发者_JS百科r file within the main() function.

If someone showed an example I'm sure I could understand it. He's shown me one with addition with the int add(int x, int y) but I want to know functions other than just simple addition. And how can I put them within the header file and then use them in main().


You use a header file by typing #include "foo.h" in your file. Typically declarations of functions go in header files while you put the definitions in cpp files.

For example:

min.h

#ifndef MIN_H
#define MIN_H

int min( int a, int b );

#endif

min.cpp

#include "min.h"

int min( int a , int b ) {  
    // return a < b ? a : b; 
    // The following does the same as the commented out line
    if ( a < b )
        return a;
    else
        return b;
}

main.cpp

#include <stdio.h>
#include "min.h"

int main( void ) {  
    printf("min( 1 , 2) == %d\n", min(1,2));

    return 0;
}

On Linux you would then compile using something like the following:

g++ main.cpp min.cpp -o minTest

which would give you an executable named minTest which you could then execute by typing ./minTest


A header file is useful when you want to use a class in a source file (.cpp) that's different from the one the class was implemented in.

To compile (create the .o object files), c++ compilers require to at least see the prototype of the functions you are trying to use; the functions need to be instantiated in some way, like any function you would normally use. The compiler wants to verify that the types of the parameters and the type of your return value coincides with the way the function is used in your code.

So, you could put

int add(int x, int y);

in a file named "addition.h" and your definition,

#include "addition.h"
int add(int x, int y)
{
    return x+y;
}

in a file named "addition.cpp".

In your main file, you would just have to..

#include "addition.h"

and you could use addition(int,int) as much as you would like, as long as you don't forget to compile addition.cpp with your main.cpp, in a manner similar to this: (this example uses G++)

$ g++ -c addition.cpp;
$ g++ -c main.cpp;
$ g++ main.o addition.o -o BinaryOutput.out

I hope this helps.


In C++, you need to understand the difference between function declarations and function definitions.

  • A declaration says "a function called somename exists and this is its interface".

    extern int somename(int x, int y);
    
  • A definition says "a function called somename exists and this is how it is implemented".

    int somename(int x, int y)
    {
        return x + y + 1;
    }
    

A header is typically used to specify the interface provided by one or more classes, or the interface provided by one or more functions. It may provide other information that users of the code need to know. A header is intended for use by more than one source file; there isn't a lot of point in having a header that is intended for use by a single source file (though there are some cases where it makes sense).

So, normally, a header provides declarations of functions.

To complicate matters, functions can be defined inline. When a function is defined inline, then you place its implementation into a file (often a header file) and the compiler might optimize the use of the function to avoid the overhead of a function call. So you might create a header containing:

inline somename(int x, int y) { return x + y + 1; }

Without the inline keyword, this would lead to a violation of the ODR - One Definition Rule. The ODR says that a function or globally visible object may only be defined once in the program. If the keyword inline was omitted in the header, but the header was used in more than file, then each file would define somename() and the files could not all be linked together because there would be multiple definitions of somename().

0

精彩评论

暂无评论...
验证码 换一张
取 消