Log in

View Full Version : something about programming languages


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?

gabest
14th October 2003, 19:27
Using header files have a few advantages. For example if you change the cpp file, the dependent cpp files don't need to be recompiled. Then there is the interface/implementation separation. You can give the definition to others and they won't have to dig themselves through the belonging thousands of code lines. And if you are using COM classes, you won't even have to let them know the protected/private class members and all the variables, just the interfaces.

Prettz
14th October 2003, 19:34
Have you ever seen an average .cpp file? I don't know about you but I'd rather keep the hundreds or even thousands of lines of code out of my class definition. I always thought that defining your functions inside the class definition made the class definition look ugly, but that's only my opinion.

Doom9
14th October 2003, 21:40
well.. interfaces also exist in java and c#, so the interface part is the same. And using (D)COM(+) C++ uses .h files as interface definitions as well.

DaveEL gave me a nice explanation:
DaveEL says:
defining your code inside the class definitions makes the code inlined so the compiler handles it in different ways mostly its just a way to make compilers easier to write especially on older machines which are slower with less memory as you can only refer to something after it has been declared so you don't need to parse the entire file and then try to work out what everything was refering too you just pass over the file once and split out an error when you get something you don't recognise btw pascal is much the same you just define stuff in the same file as you code them, the .h file in the interface section and the .c files are the implementation section the reason to share the "interfaces" in c++ and not in say java is that java classes are self describing whereas c++ ones are not they are just a big collection of code basically and you need the interfaces to work out where all the code entry points are
so basically with java you implicitly share the interface whereas with c++ you have to explicitly do it its just c++ giving you more freedom
Looks like the course I skipped because I was studying abroad for a year was the one where I'd have learned the answer to my question (I never took the course where you had to learn and build compilers.. I compensated with a bit more of OS basics and databases).