-1

I can't find a purpose for a header file. why do we need it? there is no headers in C#, you just write "using", that's it.

also, what is the different between a header file and a cpp file?

thanks.

Delights
  • 349
  • 4
  • 10
  • Possible duplicate: http://stackoverflow.com/questions/333889/in-c-why-have-header-files-and-cpp-files – rsc Sep 27 '12 at 22:23
  • -1; This is something that could be trivially answered by reading any reference material on programming C++. – fluffy Sep 27 '12 at 23:32

4 Answers4

3

Answering the 'what is the purpose of a header file' part than no one has talked about, separating the method definition from the actual implementation allows the compiler to not recompile all the files in the project when making a change on the implementation of a method.

This is because the files that call it, still call it in the same way (this is defined in the header), and if the implementation changes(in the cpp file) the compiler only compiles the file that was changed and do the linking again thus saving a lot of time when working on a big project.

Topo
  • 4,783
  • 9
  • 48
  • 70
1

Take a look at the following article that explains how c# compiler works and why it doesn't need headers.

  • Instead of simply posting a link to an external site, it would be better to summarize its salient points here (but still link to the original, of course). – fluffy Sep 27 '12 at 23:33
0

AFAIK there is no difference. You can always write code without headers at all:

a.cpp

class A {
public:
   void foo();
};

void A::foo() {}

b.cpp

class A {
public:
   void foo();
};

int main() { A a; a.foo(); }

However it is just easier, convenient and produce less errors to use header file for common definitions which otherwise had to be written in each translation unit (.cpp):

a.h

class A {
public:
   void foo();
};

a.cpp

#include "a.h"
void A::foo() {}

b.cpp

#include "a.h"

int main() { A a; a.foo(); }

And of course you only compile .cpp files. However there is "precompilation" for headers.


Why C# does not need headers? I look at it quite differently. In C# you have only headers, and using is just equivalent of C++ include. But this is only my personal opinion :D

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
0

You need to have a better understanding of .NET, C# and IL. It is not as simple as "using" directive. IL is riddled with circular dependency problem and as per my last knowledge, Microsoft was trying some clever hacks to resolve the circular dependency but none of them were perfect.

Maybe when you graduate to big projects you will encounter the circular dependency problem. Till that time, just remember that CPP/HPP distinction helps avoid circular dependency...

Apeirogon Prime
  • 1,218
  • 13
  • 25