Using classes, logistics

This is a discussion on Using classes, logistics within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a very simple question. I need to include a class for a particular method. Now, I know ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Using classes, logistics

    Hello,

    I have a very simple question.

    I need to include a class for a particular method.

    Now, I know how to create a class, I know how to split a class between a header for its declaration and its methods using a header. I have even read how to use #IF #ENDIF macros to only include headers once.

    But I would be really interested to hear what people reckon is the best way to put all of this together for access from a main class.

    My main class is sprawling because I have a menu method (for main) to a load of different methods, and it is only a sub-sub-sub method to this class that needs to use the class.

    I want to grow the method more, with more classes, so I really looking for housekeeping advice here - the best way to separate parts while keeping things clear.

    Most of my experience is in Java, where it is simply a matter of throwing everything in the same folder.

    So, if someone could offer a simple template for how I should structure class definitions in a main class I would be very grateful.

    So, should I declare the class in the main file? Should I declare it in a separate class?
    Should I split in between a cpp file and a h file?

    If so, do I only need to include the header?

    A tiny bit of sample code would be excellent.

    Thanks!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Typically for projects I do this.

    Code:
    MyClass.h
    MyClass.cpp
    
    MyClass2.h
    MyClass2.cpp
    
    main.cpp
    And the main.cpp would look like
    Code:
    #include "MyClass.h"
    #include "MyClass2.h"
    
    int main()
    {
        MyClass a;
        MyClass2 b;
    
        return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Thanks p-b,

    That's pretty much what I hoped for.

    I am wondering, however, what is the mechanism and the rationale behind only including the header. It seems we include the header in the cpp and main and it kind of bridges the two?

    Is there a reasonably easy way of explaining how the compiler does this?

    (ps, thoughts to tucson btw)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How much do you know about what compilation actually is? You don't #include code, you link object files together.

    Linker (computing) - Wikipedia, the free encyclopedia

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Well if you are using a IDE(Visual Studio, Code Blocks, etc). It will handle linking all the compiled code files together for you.

    Otherwise you would have to specify all the compiled files to the linker to generate your executable.

    But basically you just need the header so that the compiler knows that these things exist and you aren't screwing up.

    (ps, thoughts to tucson btw)
    Sad, very sad. I live about 3 miles from that safeway. Drive past it all the time on the way to visit parents who live a little north. Crazy.
    Woop?

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Thanks lads,

    Yep, I am using VS, and I am ashamed to say that I have never even heard of a linker before, and my knowledge of compilers is almost equally non-existent (although I have heard of them!).

    But I get the surface logic of headers etc now.

    Thanks again,

    J

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The correct way to organize source code into classes
    By MathFan in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2010, 12:25 PM
  2. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 04:03 PM
  3. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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