Can someone tell me the benefit of header files in C++? I believe there must be good reason for it, but I don't quite understand why. Why would I want to create some code in two seperate files, somewhat repetitive, when I can write it all one time, in one place, and with less code like in so many other languages?

Wouldn't the following be much easier to write, manage, understand, and be more efficient than using a header file and implementation file?

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

class MyClass
{
	public:
		MyClass( ) // constructor
		{
			msg = "Hello World";
		}

		void MyMethod( )
		{
			cout << msg << endl;
		}
		
		~MyClass( ) { } // destructor

	private:
		string msg;
};

int main (void)
{
	MyClass test;
	test.MyMethod(); // display Hello World

	_getch(); // pause before exit

	return (0);
}

...instead of doing this, which seems like twice the work:

// MyClass.h

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

class MyClass
{
	public:
		MyClass( ); // constructor

		void MyMethod( );
		
		~MyClass( ); // destructor

	private:
		string msg;
};
// MyClass.cpp

#include "MyClass.h"

MyClass::MyClass()
{
	msg = "Hello World";
}

void MyClass::MyMethod()
{
	cout << msg << endl;
}

MyClass::~MyClass() 
{ 
	// destructor code here
}

int main (void)
{
	MyClass test;
	test.MyMethod();

	_getch(); // pause before exit

	return (0);
}

If anyone can point out to me why it would be more advantageous to use header/implementation files instead of creating it in one place, I would really appreciate it. Thanks!!

This would defeat the purpose of libraries. When moving all code into header files, it would be included in all programs that use it, instead of separate .dll or .so files.
When bugs are fixed in a library, you couldn't just replace the shared library - you would have to rebuild all applications that use it.
Needless to say, it would also greatly increase compilation times and executable size of all programs.
Another problem arises when two classes access each other. In that case, both class structures need to be known before implementing any class methods.

However, header-only template libraries do exist. Most boost libraries are header-only, actually.

Thanks for your reply Aranarth!

Ok so I definitely get the purpose of libraries. Lord knows I've used my share in other languages, so that makes total sense. I guess my question is more, when writing a class, for example, is there any reason why I would just simply want to "define" my class structure in a header, but not the functionality (like I did in my second example)? I don't see the point in seperating the definition and body/functionality of a class into two files. I would think either you write it all in the header, or write it all in the implementation file. So I was wondering if there would be a good reason for seperating the two?

In the example you posted in your OP, it makes sense to put it all in one file because there is so little there. But what if you are defining a class that has a substantial number of members. Let's say this class has 150 member methods and takes 250 lines just to define the class. You still need to implement the 150 member methods. Assuming an average length of 15 lines per method, that would be an additional 2,250 lines (2,500 total lines).

If you need to make a change to a method, would you rather wade through all 2,500 lines that serve a wide variety of purposes or 2,250 lines that all implement the defined functionality? It's a code maintenance thing.

Also, Have you ever opened these files?

#include <iostream>
#include <string>
#include <conio.h>

These are all extremely large headers. Would you like to copy all that code into every program you write? Try working your way through them and making sense of what they have in them...

Ideally, the header only contains declarations.
With the line
int square(int val);
you tell the compiler that somewhere a function square is defined that returns an int and takes an int as a parameter.
With that, you can now use the function square in your following code. The compiler doesn't care or need to know where the function is defined. This is something the linker will take care of later.
In case you don't know how the linking process works, you should familiarize yourself with it.

Same with declaring a class. You just tell the compiler how a class of MyClass looks and what kind of functions it has. It is important to note that the compiler does not generate any code for declarations.
So when including a header file, this is just to inform the compiler of the existence of a whole bunch of functions and classes. The definition of said functions and class methods can exist in any translation unit, including in external libraries that you don't even have the source code of. The linker will search the functions in all object files that you're linking together.

The binding that is usually done by the linker and the operating system (which loads the necessary libraries before running the program) can even be done at runtime, which allows plugins to access functions and objects of the main program that includes the plugin, for example.
This is a bit more advanced, though.

Ok thanks guys. I think I have a much better idea about the purpose of header files. I think I'll take a look at those libraries and see how they are written in C++.

Thanks!

prevents u from rewriting code multiple times, helps code maintenance, use can declare header files in any of yours files, without them you really coudnt use cin, cout, etc unless you made your own header files

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.