Doom9
14th October 2003, 18:58
First the background: I've started out with MS Basic, learned Pascal and OO Pascal, then I had a quick (and ugly) run in with dos C++ (Interrupt programming.. PC crashed every 2nd time I tried something), and Visual Basic. At university we studied Java and after Pascal it was the 2nd language I got pretty proficient in (of course there's still tons I don't know to this date, but I'm able to learn when required ;). 2nd year was C and I always kept wondering why the heck do I have to declare my methods before I implement them? Is this because C compilers are inheritently stupid? I mean, if you have multiple programs or classes using the same functionality you have either interfaces (for me .h files in C/C++ are the closest thing to an interface), or inheritance, but if you don't share the code, why share the definition? I wrote the prototype for my thesis in C# thus learning the whole .NET thing and now I have to code in C++ for my job. So I'm playing around in Visual Studio's C++ part and I start wondering. First I tried out managed C++, which is pretty straighforward to me because I know the API already. But I still have .cpp and .h files, and the weirdest thing is that VS writes the code in the .h files. If I create a regular C++ project (for instance an MFC project), the code is a bit divided between .cpp and .h files which confuses the hell out of me (I guess that's the part I never picked up when I read a book about Windows programming in VS a couple of months back). But C++ is object oriented and since I've done OO programming in 4 languages now I'm really wondering why the heck I have to declare methods outside of my classes (and it's different in the other OO languages I learned). I suppose many of you guys are older than me and have a lot more experience with old school programming languages where I'm mostly tending towards modern languages, so I'm wondering if anybody could tell me why we need header files and why we have to declare methods before implementing them, even if they are not shared. Is the linker not able to find methods in other classes?
Suppose I have a class A and a class B, and B is instantiated in a method in A. In my opinion, A should then have automatic access to all of B's public variables and methods without those having explicitly declared (in traditional C++ you'd have a header file that is in the #include lines of class A where those methods are declared). Why is that special declaration necessary in C(++) if it's not necessary in languages like Java, C#, VB or Delphi?
Suppose I have a class A and a class B, and B is instantiated in a method in A. In my opinion, A should then have automatic access to all of B's public variables and methods without those having explicitly declared (in traditional C++ you'd have a header file that is in the #include lines of class A where those methods are declared). Why is that special declaration necessary in C(++) if it's not necessary in languages like Java, C#, VB or Delphi?