Class decleation in files. (??)

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Class declaration in files. (??)

      Hello,

      I was taught that when declaring a class you put the
      code in one file (.h) and the methods implementations in
      another file (.cpp). I noticed that the author has placed
      both class declaration and methods in one file. Going by
      what I have been taught, I am splitting these up in
      different files, for example

      class IPacket
      {
      // Code here
      };

      and save in the file called "IPacket.h". Now I will write the implementation,


      #include "IPacket.h"
      class BinaryPacket : public IPacket
      {
      inline BinaryPacket(char const * const data, u_long size);
      // More Code here&

      };
      Save the file in "BinPacket.h".


      #include "BinPacket.h"

      inline BinaryPacket::BinaryPacket(char const * const data, u_long size)
      {
      // Code here.
      }


      and save this file in BinPack.cpp. I keep doing this for all
      classes and implementations, but I end up with errors
      when compiling. I am assuming that when compiling, it
      will read all the includes files and compile it as if it were in
      one big file, right?

      Also, this brings me to another question, is it 'standard
      practice' to write the class AND the methods in one file,
      or split them up as I am trying to do? I understand that
      you will have tons (as if files have mass and can be
      weighed) of files, but is it better?

      Thank you
      S.

      The post was edited 2 times, last by Sabrina ().

    • The author was trying to save paper.... since the book was already getting a bit unwieldy.

      Sabrina, you were taught a good practice to separate declarations (.h) and implementations (.cpp), but that doesn't mean that you should always do that separation no matter what. The reason people do this separation is...
      • make it easy for people (including yourself) to just read .h files to understand a class without all the actual code
      • when writing libs and dlls, the .h file is the only means of letting users of that lib and dll to use the functionality embedded inside it
      • separating declarations from implementations allow you to #include a files that declare things but don't define things... particularly static variables and globals (if you're into that sort of thing... that's another discussion). This #include issue is important, but you are not allowed to define something twice.


      So why would you not want to separate a class between a cpp and an h? Inlining. Putting everything in the h gives the compiler the option to inline that code, which makes it run slighter faster than assuming the code will never be inlined, which will be the case if you place the body in another file.

      So is it better to separate them? Not always, but sometimes (sorry, I hate those kinds of fuzzy answers). There might be some guidelines somewhere, but my general rule of thumb is to always separate them except for simple accessor and modifier functions such as 'GetFoo' and 'SetFoo'. Simple functions like this have a higher chance of being inlined.

      I keep doing this for all classes and implementations, but I end up with errors when compiling. I am assuming that when compiling, it will read all the includes files and compile it as if it were in one big file, right?

      It should. Yikes... I just posted this message right after you posted the error. Can you post the full description of those errors that say which file they came from? I'll need to know what the offending lines look like.

      The post was edited 2 times, last by Kain ().

    • Hi Kain.

      Thank you again for taking the time to reply. :)

      I know it might have seemed a 'picky' question, but
      since you and MrMike have had experience in the
      programming industry, I thought it would be a good
      question to ask to get some idea on how to 'standardize'
      my code.

      Anyway, I was able to resolve the compile error
      mentioned. It sure helps IF I have ALL the includes
      needed. (I forgot the winsocket2.h)


      Now that I have read the chapter 16 on "Network
      Programming" (actually Im reading it for the 3rd time
      now) I am writing the code, instead of the old
      copy-and-past routine from the authors source code, (I
      feel this will help understand the code and sockets much
      better!) I made a "main()" function to test and see how
      the code works, but I am not sure how to do this. I am
      assuming from the authors code, I can use the following
      to test;

      ClientSocketManager *pClient = new ClientSocketManager(_T("www.blah.com",1234);

      /// Hummm&.. Now what. How do I send a message?

      This code sets up a connection (binds, connects, etc).
      I'm not clear on how I would be able to send to the
      listening server Hello there. I'm kinda struggling right
      now understanding the "Event System". I really dont
      want to ask about that right now until I have some time
      to re-read it again. But, if I want to send "Hello there" to
      a listening server (pretty much the same code, but for
      the server side), is there a way I can do that?

      Thank you again.

      Sabrina.
    • Hi Kain.

      Thank you again for taking the time to reply. :)

      I know it might have seemed a 'picky' question, but
      since you and MrMike have had experience in the
      programming industry, I thought it would be a good
      question to ask to get some idea on how to 'standardize'
      my code.

      Anyway, I was able to resolve the compile error
      mentioned. It sure helps IF I have ALL the includes
      needed. (I forgot the winsocket2.h)


      Now that I have read the chapter 16 on "Network
      Programming" (actually I'm reading it for the 3rd time
      now) I am writing the code, instead of the old
      copy-and-past routine from the authors source code, (I
      feel this will help understand the code and sockets much
      better!) I made a "main()" function to test and see how
      the code works, but I am not sure how to do this. I am
      assuming from the authors code, I can use the following
      to test;

      ClientSocketManager *pClient = new ClientSocketManager(_T("www.blah.com",1234);

      /// Hummm&.. Now what. How do I send a message?

      This code sets up a connection (binds, connects, etc).
      I'm not clear on how I would be able to send to the
      listening server "Hello there". I'm kinda struggling right
      now understanding the "Event System". I really don't
      want to ask about that right now until I have some time
      to re-read it again. But, if I want to send "Hello there" to
      a listening server (pretty much the same code, but for
      the server side), is there a way I can do that?

      Thank you again.

      Sabrina.
    • RE: Class declaration in files. (??)

      Hey there - it's a good question though, and even if you've answered it yourself please post the answer, just in case anyone else has a similar question!

      Thanks!
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Avoiding the copy and paste routine is a very good idea. You're definitely going about this the right way.

      I'm not sure how far you've gotten yet, but if this is your first time dealing with network programming, than I'd like to suggest a different starting point than the network programming chapter of MrMike's book.

      tangentsoft.net/wskfaq/examples/basics/index.html

      This is where I started. Specifically, the Basic Blocking Client and Basic Blocking Server will give you the simplest possible framework for setting up your client and server test applications. This should give you a nice playground to test out the rules of sockets programming in a relatively simple framework.

      Once you feel comfortable sending a "Hello There" message from one application to another using a direct one to one socket connection without the use of any C++ classes, and you understand the basics of sockets programming from that excellent somewhat official website, then you should come back to the network programming chapter (Chapter 16) in MrMike's book. Now you are ready to start thinking about the larger framework of having a flexible network system that can handle X number of connections in all sorts of flexible ways by implementing an architecture like the one diagrammed on page 606.