Thread: inheredance

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    inheredance

    Hi ! I try to break my c++ programe to .H and .cpp files. But my programe use heredity, and whene i break the prog into files I do not know how i will do it . Also my g++ compiler warning me in constractor line redwfinition. Any one who knows?

    Thanks. Sorry for my english

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    put each class in a seperate .H and .cpp's, and have child classes include the header file of its parent class.

    Code:
    // ChildClass.H
    #include "ParentClass.H"
    
    class ChildClass : public ParentClass 
    {};
    Or you can use a forward declaration
    Code:
    // ChildClass.H
    class ParentClass;
    
    class ChildClass : public ParentClass 
    {};
    Use one or the other, not both.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Or you can use a forward declaration
    You cannot use a forward declaration for inheritance. The first example is the way to go.

    As for the redefinition warning/error, you should copy and paste the error message and the lines of code it points to so we can see it.

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You cannot use a forward declaration for inheritance.
    Good call.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    I found it

    I put
    ---------------------------
    #ifdef _class
    #define _class

    <class code>



    #endif

    ---------------------------

    Thanks for the help boys.

    I will try yours way too.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    That should be:

    #ifndef

    So you know what's going on:

    Code:
    #ifndef  _class_h_   // if _class_h_ is not defined
                         // Do everything following...
    #define _class_h_    // define _class_h_
    
    // Class code
    
    #endif // end of the if statement
    Last edited by jverkoey; 05-16-2006 at 06:31 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> #ifndef _class

    Generally, symbols used for include guards are done in all capital letters. Also, words with an underscore followed by a capital letter are reserved for use by your compiler implementation. So I would use something like CLASS_H or CLASS_H_.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    Thanks boys.

    I want to know more about preprocessors directives in c and c++.
    Where can I find tutorials?

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Do a google, or get a good book. As a side note, try looking at the header files in your compiler directory folder. It is not advised that you change the code, but looking at it will give you a good platform

  10. #10
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    Thanks

    Thanks

Popular pages Recent additions subscribe to a feed