We help IT Professionals succeed at work.

c++ program structure header/source files?

mattososky
mattososky asked
on
226 Views
Last Modified: 2010-04-01
Learning c++, coming from .net / java background.

Why is the purpose of placing the signatures of functions in a header file while placing the actual definition in another source file. This does make sense, seems redundent, but two books on learning c++ have described this approach.

if i am reading and understanding correctly, they advise to do something like this:

____________________________________
//FILE 1 - Header file for ADD_FUNCTION

public int add_numbers(int number1, int number2);

_____________________________________
_____________________________________
//FILE2 - source file for ADD_FUNCTION
public int add_numbers(int number1, int number2){
      return number1 + number2;
}
_____________________________________
____________________________________
//FILE 3 - main program.

int main(args[]){
       
       cout << add_function(2,2);


}

__________________________________________

we do somthing like this? why not just have only source file, or why not include the definition in the header file?










Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Let's try that again - I promise I haven't been drinking!  In fact I don't know what happened - so I'll try and tidy that up and add a bit more - fingers crossed!

Lots of reasons:

The main reason is that one often wants to separate definition from declaration - in fact, it's often the case that one is forced to do that - see below, i.e., you *can't* have the implementation.


1. Think about the OS you're writing code on, as a 'lead in'.

They [MS if you want] don't want you to have access to the source code of Windows, so they define *most* of the various function-signatures and structs they use in a header [which they're happy to give you] - you need those so you can compile code *against* those binaries that you can't actually build yourself.

Now move closer to home.

2. You - and others in your team might want to control their own bits of a project - and are *only happy* to let others have pre-built bits - these 'others' get the 'the bits' they're *not* responsible for but that they *need* for their own bits to work ... after all - you know that if they give them the source - they *will* tinker with it - or worry about etc [or break it!]   So, you give them compiled libs or objs of one sort or another, but also *provide them with the means* to code against and use those libs etc.

2a.  How about if you change the implementation of some of your bits - but others have the source!  How can you *know* that they're building with the latest stuff?  A. Don't give 'em the source - give 'em a binary.  Your interfaces stay the same of course - they're defined in your headers.  You might add some more of course - that's what all the Windows' ???Ex stuff is about.

3. There are - and most of these are for 'protection' - other times when all of this [above] applies.  For example, research Opaque Structs for a blinding example of just how great it is to have function sigs and structure defs elsewhere to the source itself.  Really - if you're going to do nothing else - have a look for these!  Windows - and all other OSs - are littered with these - and for very good reasons.


There - hope that sounds a bit more structured!
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
cuziyg's comment

>>It's a tedious task to maintain separate header and code files for a small project.  That's why it doesn't seem to make sense.  But as soon as you work on a larger project and have to collaborate with others on it, headers are the only way to go.

That's about the best bottom line you'll get on this subject.

Author

Commented:
are there such things as interfaces in c++ then, or is that java / .net specific and header files in c++ are the implementation of that functionality?

and/or what is the relationship betweet java.c# interfaces and c++ header files?

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Well, an *interface* in terms of OO is different to an interface to a moduke. The characteristics are declarations with pure virtual methods, e.g.

struct IVehicle  {

virtual void Drive () = 0;
};

class Car : public IVehicle {

void Drive (); // implement Drive for cars
};

class Train : public IVehicle {

void Drive (); // implement Drive for trains
};

Commented:
The term 'interface' or its plural is often reserved for the more pure OO languages - like Java, C# and - if you must - C++ [I add what might seem like a pejorative wink there because C++ isn't, IMHO, 'pure']

However, the term is subjective and I wouldn't slap down any programmer that used it - as long as I didn't think the context was misleading.

Wikipedia's opening slavo on the term is "An interface defines the communication boundary between two entities", and I'd argue that one could quite easily label a C header file as such an entity.  Foldoc [http://foldoc.org/index.cgi?query=interface&action=Search] has the similar [!] "A boundary across which two systems communicate" -- so, 'ditto' there.



Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.