Page 1 of 1

What's the difference between...

Posted: Fri Mar 06, 2009 8:39 pm
by Rarsonic
The headers (public) and the sourcecode?

Kind-of a noob question, I know... :P

Re: What's the difference between...

Posted: Tue Mar 10, 2009 4:26 pm
by Smartball
Although it's possible to put source code in header files, they are typically used for putting declarations in without giving any detail as to the implementation. The implementation code is then put in source files. This allows you to know what sorts of functions you can call on certain classes without having to know how they are implemented, and also allows you to declare something in one header file and then reuse it in multiple source files. So if I provide a header file and a compiled source library, you can use my header file to know how to interface with the classes in my library without having to know how I implemented any of it. The real gain is greater encapsulation and code reusability.

Re: What's the difference between...

Posted: Wed Mar 11, 2009 7:36 pm
by [§Ŕ] ŤhěxĐâŕkśîđěŕ
So, in Java aspect, header=interface/abstract class and source sode=class?

Re: What's the difference between...

Posted: Thu Mar 12, 2009 12:05 am
by Bane
I suppose that's a decent analogy, except you have headers for (just about) every function, while you don't need every class to be the subclass of an abstract class.

For the most part, headers are just the function and class definitions, but with out the body of the function.
Like "int incrementByOne(int n);"
instead of

Code: Select all

int incrementByOne(int n)
n = n + 1;
return n;
Basically you can see the name and call the function, but you can't see exactly how it works (you're left at the mercy of the documentation).

Re: What's the difference between...

Posted: Thu Mar 12, 2009 12:25 am
by Smartball
So, in Java aspect, header=interface/abstract class and source sode=class?
Not exactly. Java makes no distinction between a header and source. The idea of a header file is not comparable to Java's interfaces and abstract classes because interfaces and abstract classes are actual parts of the programming language, whereas a header file is a list of declarations. They exist for fundamentally different purposes. You can't "implement" or inherit from a header file, that doesn't make sense. The header provides information to the compiler to let it know what it's allowed to do with the compiled code.

In Java, a declaration cannot exist without a definition. The only exceptions to this rule are declaring a method with no body inside of an interface or declaring an abstract method. However, this still does not make interfaces comparable to header files because they are setting up a base for derived classes to inherit from. They are not necessarily specifying an API for something that exists. And furthermore, the interfaces and abstract classes that something may inherit from are not distributed separately from the rest of the compiled code - the reason being, abstract classes and interfaces ARE source code in Java.

When you want to use someone else's compiled Java code, it typically requires you to read some sort of documentation to see what the classes' APIs are (I'm speaking in principle, this is ignoring the fact that java bytecode is decompilable). Distributing a C++ library requires the inclusion of compiled code and header files, because C++ makes a strong distinction between a declaration and a definition. Without the header files, a C++ compiler can't use the compiled code because none of it would be declared. A Java library just requires the inclusion of compiled code because there is no such thing as separating a declaration from its definition in Java. So, as I said, to know how to use that compiled code, you'd need some sort of documentation explaining the available API. It's always nice to have additional documentation for a C++ API as well, but that documentation can exist in the header files themselves.

Re: What's the difference between...

Posted: Thu Mar 12, 2009 8:33 am
by GreatEmerald
So basically we can't see the Source Code, but we can see headers, even here on our forums: http://www.oldunreal.com/cgi-bin/yabb2/YaBB.pl?num=1202460562/60#60