DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9
  1. #1
    Join Date
    May 2005

    Header Files compared to Java

    Ive been doing java for a while, and am now learning C++. I am confused when it comes to a .h or header file. In java there is no such thing, and im wondering if someone could explain to me what they are for. The explanations in the books im reading say they are for prototype functions, aka abstract methods if your talking in java terms. Is a header in C++ like an Interface in java??

    If you good at explaining stuff please respond with your 2 cents. thanks

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    That's a good starting off point for thinking about the function of a header file. However, there is no "type" given by a header file, the way (in Java) that any class which implements a "Position" interface is of a "Position" type.

    The "interface" nature of a header file is that it is the "public information" being shared about the compiled object file which defines the methods declared in that header file. It provides the means for passing messages - the proper format of method calls - which an "outsider" can send to an object file and what kind of messages it will get in return. It gives the user - and, most importantly, the compiler - enough knowledge about the object file that when the time comes to call any method in the object file you (it) will be able to do so.

  3. #3
    Join Date
    Nov 2003
    In C++, you have to declare everything before you can use it: variables, functions, classes, templates or constants. Because C++ supports the seprate compilation model (i.e., you can compile several .cpp files separately and then link the object files to an .exe), you need a mechanism to enable the compiler to recognize classes, member functions etc. that are implemented in a separet .cpp that is compiled seprately. Theoretcially, you could use a Java like mechanism which loads the relevant .cpp but this is very inefficient and besides, not always do you have the .cpp file at your disposal -- for example, when using a shared-library. Thus, a header file is a place in which you provide declarations, but not definitions, of entities that may used in other .cpp files. In a way, a header file is a means of exporting, or sharing information about classes, member functions and free functions withoyt providing their definition (implementation). A good header file is usually coherent, meaning it contains declarations of a single class, a single set of related functions (say fopen_socket, close_socket, read_socket) etc. If the terms declarations and definitions confuse you (they do tend to confuse even experienced programmers), you can refer to this article: http://www.informit.com/guides/conte...lus&seqNum=188

    Finally, interfaces and header files aren'te exactly the same thing. In Java an interface is essentially what C++ calls an abstract class; a header file however may contain more than one class (look at <fstearm> for example which includes various types of file classes) so you might compare a header file to a package in Java, although again, a header file is something that's unique to C and C++.
    Last edited by Danny; 11-15-2005 at 11:21 AM.
    Danny Kalev

  4. #4
    Join Date
    Dec 2003
    Adding to that they provide multi-language support. If you have a valid library or dll for you compiler, made from any language, all you need is a .h that c++ can parse to use the library in your program.

    Also note that you can only 'include' one item one time. So most header files have 'include guards' to protect against this. It looks like:

    #ifndef filename_h //if not defined (filename_h is one convention)
    #define filename_h //then define it so next time, the if not defined fails



    A "good" header file defines the functions the (other programmers) will be using once the files are *done*. Therefore it is a really, really good place for your overview comments (this class does X. Routine Y has this bug. Sample use: etc)

  5. #5
    Join Date
    Sep 2005
    istanbul / Turkey
    java is more suitable for OOP.
    you can not hide private elements in C++ header files.
    modifying private fields as public in header files
    and accessing taht fields ( in library file ) is possible.
    a C++ programmer adviced a technic,
    (keywords :cheshire cat idiom or compiler firewall idiom or pimpl idiom . http://www.gotw.ca/publications/mill05.htm )
    for me, its not totaly safe .
    it give a change to hide private content. ( becos private names are not available in header ).
    but a pointer to private content is still available, its possible to make it public.
    then user can delete it or even exchange private content of two different object.

  6. #6
    Join Date
    Nov 2003
    I completely diagree. The first thing we have to remember is that OOP has absolutley nothing to do with security. It's simply a better technique for designing software that is meant to be used by innocent users, not by malicious crackers. If you're looking for data hiding, Java isn't exactly an ideal choice either because it's easier to decompile bytecode (something you can't accomplish easily when using optimized .exe files, for instance), so let's not confuse security with OOP.

    Secondly, the Pimpl idiom is one good approach to hiding private data members but the point is not to hide information so that people can't find what the class contains but rather to minimize compile time dependencies. Finally, header files have nothing to do with security either, but if you really insist on hiding information from users you can use local classes and static free functions in C++. But remember: if you want to hide implementation details, you need special software tools such as encryption, obfuscators and so on. Header files are just a means of sharing declarations among different separately compiled modules.

    Finally, perhaps Java is "more suitable for OOP" (not that I would endrorse this claim -- it doesn't have multiple inheritance for instance, which many good OOP languages do support), but in C++ the current trend is generic programming, so the classic notions of OOP are pretty outdated in C++ today. You still have classes, constructors, private data and so on but you will see fewer virtual functions and inheritance, much more templates. In short, what I'm saying is that moving to C++ requires a paradigm shift and a different way of thinking. Many Java programmers migrating to C++ try to write Java in C++ keywords, which is never as good as *thinking* in C++. But you have plenty of time to master templates and the STL...
    Last edited by Danny; 11-16-2005 at 11:25 AM.
    Danny Kalev

  7. #7
    Join Date
    Sep 2005
    istanbul / Turkey
    i agree OOP mostly involved in design stage.
    but i am not sure if its a security or design issue,
    for me, there is not any pratical or teorical difference between an orginal header file and modified one.
    at least its a big temptation doing wrong things :D and
    breaking open-closed principle.

    another issue in C++ headers is avoding circular includes.
    ( a.h include b.h , b.h include a.h ...) which may effect design.
    i dont know, if import (in steed of include )is not possible in C++ someway.

    i liked java, it save my coding time
    but it required more time for playing with design ideas.

  8. #8
    Join Date
    May 2005
    Java does have multiple inheritance in the form of Interfaces. Its a little bit different i guess, but it gets the job done just as good if not better.

  9. #9
    Join Date
    Nov 2003
    I don't understand how can it better if it's excactkly the same as inheriting from abstract classes. Java's interface inheritance is precisely that. However, if you examone standard libraries such as <fstream>, <iostream> and <sstream> you will see how powerfull real multiple inheritance is. I remember how annoying it was to use the DataIputStream and DataOutputStream classes in Java back in the late 1990s, because they lacked the symmetry of the C++ I/O classes. And this symmetry is the result of using multiple inheritance properly.
    Last edited by Danny; 11-18-2005 at 08:48 AM.
    Danny Kalev

Similar Threads

  1. Replies: 8
    Last Post: 04-02-2005, 01:06 PM
  2. Replies: 0
    Last Post: 06-10-2002, 09:19 PM
  3. XML, Java and not using files
    By chris in forum XML
    Replies: 5
    Last Post: 07-09-2001, 09:16 AM
  4. Java...how does it all start?
    By Mike Skvarenina in forum Java
    Replies: 1
    Last Post: 02-09-2001, 12:04 AM
  5. Replies: 1
    Last Post: 03-22-2000, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.