Thread: So Now Im getting confused?!?!?

  1. #1
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Question So Now Im getting confused?!?!?

    With the class thing and how you use the .h and the .cpp. So far my understanding is that you have:

    Main C++ program, <<With an header file<< class.h <<The header File links to the .cpp file.

    See what I am saying? It seems that there is wasted step here. Why not have the Main program just link to the exta .cpp file or put the class in the .h and not just prototypes??

    I know I am confused here and this is just what is going on in my mind but please help, correct me! Thanks
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    in many ways you could do exactly that. you can declare functions in the cpp files you are using them without using a .h file.

    The fact is that in each module, you must have some concept of what the thing is you're refrencing, so the cpp has to have some definition available to it. A cpp must compile individually without knowledge of other cpp files. Why? that's how compilers work. A .h file just makes it easier to declare things in each cpp file. So if you use functions from a particular cpp in many other cpp files, you can just #include the declarations on one line.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    So, I could put a main C++ and have it referance to a differant .cpp that can compile on its own, what is the point of having a header that just has prototypes in it, when I could just put those prototypes in extra C++ file.
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Another consideration is that when you distribute your program you can distribute the .h files in text format so users can interface with your program whereas the .cpp files can be distributed in a format users can't read so easily so they are less likely to try to change your implementation and possibly screw the whole thing up in the process.

  5. #5
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    If you're building a library (the programming kind) you usually give the .cpp file already compiled and ready to be linked to, and the .h file the user can #include to be able to use the definitions and not having to do a lot of extern declarations.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  6. #6
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    I am still really confused can someone start from square1 and explain this not using a lot of advanced programmin lingo?
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You should understand the difference between interface and implementation. When designing a program, you want to separate the interface from the implementation. In order to do that, you put the interface into header files and the implementation into source files.

    In C++, you tend to separate classes in this way. The interface can be seen by the class declaration in the header file. The implemetation is found as the definition in the source file. (The declaration is class Foo ( ... ); and the definition is the actual definition of the functions in that class).

    In general, classes interact with each other by calling methods that are part of the interfaces of those classes. Since in many cases one piece of code only needs to know what a function looks like so that it can be called, all you need to show it is the declaration of that function. Since the declaration is in the header file, you only need to include the header file's contents inside that source file. When the compiler compiles that source file, it just notes that a function will be called with certain arguments and that function is defined somewhere else.

    After all of the source files are compiled separately, they are linked together. So the first source file that was compiled with a reference to a function that was defined elsewhere gets to link to the source file that actually has that definition.

    This makes compiling much faster, because the code in each source file is compiled only once even if it is reused many times. If you put all the code into a single file you also get fast compilation, but with anything but a simple program it is better to separate the program logically into pieces that can be maintained and used separately.

    If you aren't familiar with classes, functions have the same property. If there is a function you like to use a lot, and your program is big enough to be put into multiple files, then its a good idea to put the declaration of that function in a header file so that all the other source files can see it. Then define the actual body of the function in a single source file so that it compiles only once. Its the same idea, separating interface from implemetation so that the interface can be used in many places but the implementation is compiled only once.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Big words.. scary
    I'll try to sum that up:

    Each .cpp file is compiled into a separate object file, so that only .cpp files that have been modified need to be re-compiled, to save time (and possibly gray hairs).

    If in the main cpp you want to use functions that are defined in other .cpp files, the compiler won't know what to do unless you tell it what parameters the function takes, and what it returns; you do this (tell the compiler the parameters and return type) by declaring the function 'prototype', like so:

    int someFunctionInAnotherCPPFile(int, char, char*, double);

    Do you remember what #include does? Basically, it just grabs whatever file you specify, and sticks it where you put the #include. That's why you put the function prototypes for .cpp files in headers, that way when you include the .h, it tells the compiler how to use the function in the other .cpp.

    When you split a class into a header and .cpp, basically you're doing the same thing... except with a class instead of a function.

    Then, you have the linker. The linker' job is to grab all the object files that the compiler made, and glue them together in a big .exe file, so that the functions are all together in a big happy family, and the main .cpp (not really a .cpp anymore, it's a .exe now) can use them all, based on the prototypes that you gave the compiler earlier.

    Hope this helps! I tried to get rid of all the confusing programming jargon that makes learning hard.
    Last edited by Hunter2; 03-04-2004 at 10:19 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Hunter2
    If in the main cpp you want to use functions that are defined in other .cpp files, the compiler won't know what to do unless you tell it what parameters the function takes, and what it returns; you do this (tell the compiler the parameters and return type) by declaring the function 'prototype', like so:
    ...
    Hope this helps! I tried to get rid of all the confusing programming jargon that makes learning hard.
    haha... nice try j/p...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol I was under the impression that he used "main cpp" himself, I guess I was wrong... he said "Main C++ program" instead

    -"functions": Err, oops, I thought that was what he asked about, didn't realize he was asking 'bout classes But my post is still relevent, I guess, and useful anyways for the interesting side knowledge...
    -"compiler": I sincerely hope he knows what that is!
    -"parameters" and "returns": If he knows what functions are, chances are he'll know what these are too
    -"declaring the function 'prototype'": I tried to make what I meant clear with the example...
    -".cpp" he used himself

    At least I tried! lol

    **EDIT** Hmm, the mood swings hit again...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    I know all the words and that helped alot thanks!
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No problem
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  3. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM