.cpp and .h files - organization

This is a discussion on .cpp and .h files - organization within the C++ Programming forums, part of the General Programming Boards category; What it the advantage of having your program organized as follows - just organization? main.cpp - the main part of ...

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question .cpp and .h files - organization

    What it the advantage of having your program organized as follows - just organization?

    main.cpp - the main part of your program
    object.h - the defitions for an object your program uses
    object.cpp - the implementation for the object, based on the definitions in the object.h file

    I've always just stuck the object's definitions AND the object's implementation in a .h file, then only had one .cpp file for my project, but I've noticed a lot of projects do it the way I mentioned above.

    And how do I compile a program like I mentioned above - like this?

    g++ main.cpp object.cpp -o main.exe

    Also, what is the difference (with GCC) between

    gcc main.cpp Person.cpp -lstdc++ -o main.exe
    and
    g++ main.cpp Person.cpp -o main.exe

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by ChadJohnson
    What it the advantage of having your program organized as follows - just organization?
    More or less: imagine you have 100 classes. And you need to fix a bug in only one.
    Quote Originally Posted by ChadJohnson
    And how do I compile a program like I mentioned above - like this?
    Look to IDEs or makefiles to help ease the pain.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    I guess my question is: how does the compiler know the definitions (thos in the .cpp file) for the objects if only the .h file is included in your program?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The header tells the compiler how things are to be used. The compiler doesn't need to know that it is correct. The linker puts it all together, and if things are amiss it will let you know.

    Much like if I tell you to double an int you don't need to know a particular value to write a function. It has a general form. But eventually you need to write code to do this otherwise someone (the linker) will complain.

    So you write a header to specify how it is used. You write a module and implement the use. You separately compile each module, and since the compiler has verified that each instance is used correctly* elsewhere, the linker can plop in a function call safely where one is desired. And the code trots merrily off to the function. [stop me I'm rambling]
    Last edited by Dave_Sinkula; 01-12-2006 at 10:48 PM. Reason: 'value write' -> 'value to write'
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Thanks, that explains it well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 11:45 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. .h files: "constant already defined"
    By wakeup in forum C++ Programming
    Replies: 11
    Last Post: 11-22-2005, 04:31 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 05:37 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21