Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

What’s a header? What’s a source-code file? Discuss the purpose of each.?

plz help!!!!

1 Answer

Relevance
  • 8 years ago

    I assume C++. This is what I understand:

    Header files are used to declare things:

    class Bob

    {

    int data;

    public:

    Bob();

    ~Bob();

    void Test();

    };

    Source files are used to define things:

    #include "Bob.h"

    Bob::Bob()

    {

    data = 2;

    {

    Bob::~Bob()

    {

    //do something

    }

    The reason why this done, instead of just putting it all the code in one file is because: Usually, if you were to change some of the code in this class, then all files that include that class would need to be rebuilt. This can be very annoying with projects that have 400+ files, where a lot of them include that class, because the compile time could become exceedingly long for even the smallest changes.

    But, when you separate the files into header files and source files then the files that include that data will only need to be rebuilt if you change the header. Changing the source file means that only that one file will need to be rebuilt.

    On a side not, you will probably want to put some header guards in there too, so there are no "class type redefinition" errors.

    class Bob; // makes all other files aware of Bob

    #ifndef BOB

    #define BOB // prevents Bob from being defined multiple times

    class Bob // defines Bob

    {

    int data;

    public:

    Bob();

    ~Bob();

    void Test();

    };

    #endif

Still have questions? Get your answers by asking now.