Help me about Header Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dharmesh Gupta

    Help me about Header Files

    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.cp p", "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
  • John Harrison

    #2
    Re: Help me about Header Files


    "dharmesh Gupta" <dharmesh.gupta @esteltelecom.c om> wrote in message
    news:457ed71a.0 307232112.54886 bed@posting.goo gle.com...[color=blue]
    > 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.cp p", "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 ,[/color]

    Right.
    [color=blue]
    > 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[/color]

    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


    Comment

    • Jonathan Mcdougall

      #3
      Re: Help me about Header Files

      >right now i have included "idr_bpl.cp p", "idr.h" and other standard[color=blue]
      >files (like iostream etc........) in bpl.cpp and[/color]

      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.cp p" which will contain the
      implementations :

      // myclass.cpp

      # include "myclass.h"

      MyClass::MyClas s(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.cp p". 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

      Comment

      • Michael Spencer

        #4
        Re: Help me about Header Files

        dharmesh.gupta@ esteltelecom.co m (dharmesh Gupta) wrote in message news:<457ed71a. 0307232112.5488 6bed@posting.go ogle.com>...[color=blue]
        >
        > can anyone help me arranging my code in proper header files, since i
        > will have to make several such programs in future.
        >[/color]

        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



        It's also at



        Hope this helps,

        Mike

        Comment

        Working...