Help me about Header Files

Discussion in 'C++' started by dharmesh Gupta, Jul 24, 2003.

  1. Hi All,

    i am new to header files , and have a basic question ,
    i have made my program in three files namely,

    idr_bpl.cpp( contains class definitions and function definitions)
    bpl.cpp( contains main() function)
    bpl.h (contains variable definition of a variable that is shared in
    the above two files )

    right now i have included "idr_bpl.cpp", "idr.h" and other standard
    files (like iostream etc........) in bpl.cpp and

    "idr.h" and other standard files in idr_bpl.cpp

    now, as suggested by one google friend that it is not a good practice
    to include a cpp file in another cpp file , i want to make header
    files for separating the definitions and implementation,
    the point to be noted is that there are 2 classes(namely body and
    header) defined in idr_bpl.cpp and the second class's(header's) one
    function uses the object of the other class(body).

    can anyone help me arranging my code in proper header files, since i
    will have to make several such programs in future.

    Thanks
    Dharmesh
    dharmesh Gupta, Jul 24, 2003
    #1
    1. Advertising

  2. "dharmesh Gupta" <> wrote in message
    news:...
    > Hi All,
    >
    > i am new to header files , and have a basic question ,
    > i have made my program in three files namely,
    >
    > idr_bpl.cpp( contains class definitions and function definitions)
    > bpl.cpp( contains main() function)
    > bpl.h (contains variable definition of a variable that is shared in
    > the above two files )
    >
    > right now i have included "idr_bpl.cpp", "idr.h" and other standard
    > files (like iostream etc........) in bpl.cpp and
    >
    > "idr.h" and other standard files in idr_bpl.cpp
    >
    > now, as suggested by one google friend that it is not a good practice
    > to include a cpp file in another cpp file ,


    Right.

    > i want to make header
    > files for separating the definitions and implementation,
    > the point to be noted is that there are 2 classes(namely body and
    > header) defined in idr_bpl.cpp and the second class's(header's) one
    > function uses the object of the other class(body).
    >
    > can anyone help me arranging my code in proper header files, since i
    > will have to make several such programs in future.
    >
    > Thanks
    > Dharmesh


    Just put the classes in the header file, but put the class functions in the
    cpp file.

    // idr.h

    class X
    {
    public:
    void a_func();
    };

    class Y
    {
    public:
    void b_func();
    };

    // idr_bpl.cpp

    void X::a_func()
    {
    Y y;
    y.b_func();
    }

    void Y::b_func()
    {
    }

    john
    John Harrison, Jul 24, 2003
    #2
    1. Advertising

  3. >right now i have included "idr_bpl.cpp", "idr.h" and other standard
    >files (like iostream etc........) in bpl.cpp and


    Bad idea. cpp files are implementation files, which are not meant to
    be included.

    The whole idea behind this is quite simple. You must understand the
    diference between a compiler and a linker. There are very good books
    out there, check out www.accu.org.

    But let's put a simple example :

    int main()
    {

    }

    Quite simple. Now you have function which you want to use :

    void f()
    {
    std::cout << "this is my function";
    }

    So you put it before main and you call it :

    void f()
    {
    std::cout << "this is my function";
    }

    int main()
    {
    f();
    }

    Now, your project is getting larger and you want to seperate the
    main() from other functions. You keep main() in your original file
    (let's call it main.cpp) and you put your function in a new one (let's
    call it function.cpp). Your main.cpp now won't even compile, since
    you put f() in another file. You have to tell your compiler that f()
    exists, but somewhere else :

    // main.cpp

    void f();

    int main()
    {
    f();
    }

    And that's it. The compiler parses both files and finds no errors.
    Then the linker kicks in, puts the two files together and the call to
    f() in main() is 'linked' to the f() in function.cpp.

    But now, let's get on with a new project :

    // myclass.h

    class MyClass
    {
    private:
    int some_stuff;

    public:
    MyClass(int s) : some_stuff(s)
    {
    }

    int stuff()
    {
    return some_stuff;
    }
    };

    This is your class and it is in a header file ("myclass.h"). This
    header is meant to be included by any file which uses this class :

    // main.cpp

    # include "myclass.h"

    int main()
    {
    MyClass my_object(5);
    }

    But now, imagine you have some twenty different files which are all
    including "myclass.h". Everything goes pretty well until you decide
    the constructor must validate the int before putting it in
    'some_stuff'. You change some code and you recompile. Since the
    header changed, all of your twenty files must be recompiled too.

    This is the reason for which you have to separate the class definition
    and the class' implementation. In your header file, you will only put
    the function's declarations :

    // myclass.h

    class MyClass
    {
    private:
    int some_stuff;

    public:
    MyClass(int s);

    int stuff();
    };

    That's it. Now your twenty files are quite happy with that since they
    only need declarations, not definitions of member functions.

    And since you have to put the implementation somewhere, you get
    another file which you call "myclass.cpp" which will contain the
    implementations :

    // myclass.cpp

    # include "myclass.h"

    MyClass::MyClass(int s) : some_stuff(s)
    {
    }

    int MyClass::stuff()
    {
    return some_stuff;
    }


    So if you have to change something in the code, the only file which
    will have to get recompiled is "myclass.cpp". Of course, if you want
    to add another member function (such as

    void stuff (int new_stuff);

    ), you will change the header file and the whole thing will recompile.
    But that is normal.


    So that is the deal : class definitions go in .h files and class
    implementation go in .cpp files. You include the .h file when you
    need that class, but you never include the .cpp file.

    And usually, you will have no more than a couple of classes in a
    header file.


    Jonathan
    Jonathan Mcdougall, Jul 24, 2003
    #3
  4. dharmesh Gupta

    Guest

    (dharmesh Gupta) wrote in message news:<>...
    [snip]
    > can anyone help me arranging my code in proper header files, since i
    > will have to make several such programs in future.


    Others have already given you some good advice. Here's some
    little "tricks" to help you get into this in the right way.

    CAVEAT! None of these rules are carved in stone. There are cases
    where breaking them is better than following them.

    The header file for a class should contain exactly the information
    required to use that class. No more and no less. The idea is, a
    client of the class needs to include the header, and it gets all
    it needs out of there. It should not require that other headers
    be included to use the class. The header is like a contract for
    using the class.

    To encourage that, follow this rule. In the implementation file of
    a class, make the first line the #include for the header for that
    class. Then, as you build, anything that will be needed by a client
    goes in the header. Anything that is only needed by the class gets
    included in the implementation file with extra #include lines
    that appear *after* the include for the header file. So, it's a
    careful sort into "needed by client" and "not needed by client."

    Use double-include guards. Look up and get to know the #define
    and #ifdef compiler directives. That way, you can safely inlcude
    standard header files in your class header files.

    Socks
    , Jul 24, 2003
    #4
  5. (dharmesh Gupta) wrote in message news:<>...
    >
    > can anyone help me arranging my code in proper header files, since i
    > will have to make several such programs in future.
    >


    You can try Lazy C++. Write your component in one file, define
    entities at their declaration (Java-style), then run lzz, a C++
    preparser. Many onerous coding tasks go away, like writing header
    include guards and removing default args from definitions. Specially
    comes in handy when you want your template definitions in a separate
    file (if you want to explicitly instantiate your templates) or
    inline function definitions in a separate file (for conditional inline
    compilation).

    You can try it out online at

    http://www.lazycplusplus.net/cgi-bin/lzzcgi

    It's also at

    http://www.lazycplusplus.com

    Hope this helps,

    Mike
    Michael Spencer, Jul 25, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    688
    lilburne
    Nov 2, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    593
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,275
    Walter Roberson
    May 1, 2006
  4. Bit byte
    Replies:
    1
    Views:
    637
    benben
    Apr 19, 2006
  5. mlt
    Replies:
    2
    Views:
    808
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page