ifndef and define statements

This is a discussion on ifndef and define statements within the C++ Programming forums, part of the General Programming Boards category; My C++ book doesn't give any specific details of how the ifndef and define statements are supposed to be used. ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    38

    ifndef and define statements

    My C++ book doesn't give any specific details of how the ifndef and define statements are supposed to be used. If anyone could give a brief summary of how to use them with a base class, a derived class, and a derived class of the derived class.
    Ex. Base->Derived->Derived.
    This information would be very highly appreciated. Thanks.
    SilasP

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It's called an inclusion guard. It simply prevents a class or function from behing defined more than once.

    #ifndef NAME
    #define NAME

    class Name { };

    #endif
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Do you put just the name of the class you are doing in the statement? EX. #ifndef ACCOUNT_CLASS
    #define ACCOUNT_CLASS
    class account
    {}
    #endif
    Thanks .
    SilasP

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    As long as the name is unique, it doesn't matter. If you wish to be proper, naming conventions will use some form of the file or class name using all caps and underscore for spacing.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    OK. But I have one more question. Suppose I have a base class named account and a derived class named chk_acct, what would be my ifndef and define statements for the derived class? Do I put both the base and the derived within them or just the derived?
    SilasP

  6. #6
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Depends on how you code. Typically, to each its own.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  7. #7
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Let me expand a bit.

    You could have both the base and derived class defined within the same header file. In this instance, you could use just one guard if you wanted. Most of the time though, each class will have its own header and source file. In these cases, you will want to separate.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    I think I understand it. All of my classes have their own header files, so I should just include the name of the header file I am creating in the ifndef and define statements. Is this right?
    SilasP

  9. #9
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Typically, yes. If I seem somewhat dodgy in my responses, it is because this topic can really be a matter of style.

    But, "typically," for use in a header file you would use the name of the file.
    Code:
    // someheader.h
    
    #ifndef SOME_HEADER_H
    #define SOME_HEADER_H
    
    // code
    
    #endif  // SOME_HEADER_H
    Non-conventional naming can lead to poor readability and reusability. Can you imagine trying to maintain code that followed this kind of logic?
    Code:
    // someheader.h
    
    #ifndef I_LIKE_PICKLES
    #define I_LIKE_PICKLES
    
    // code
    
    #endif  // I_LIKE_PICKLES

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Ok. So this really doesn't cause the inheritance to transfer to the derived from the base class. It just checks to see if it has been defined. Right?
    SilasP

  11. #11
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The inheritance transfer is guarenteed when you inherit .

    class A { };
    class B: public A { }; // B is guarenteed to have inherited A.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Is that the way it is supposed to be defined in the derived?
    Such as:
    class account {}
    class sav_acctublic account {} ?
    The way I was starting the class was going straight to the
    class sav_acctublic account with no brackets
    If you don't mind, please give me the exact start into the sav_acct class I have specified. I didn't include the ; at the end, but if it needs to be there, please tell me. Thanks for your wisdom of the C++ language.
    SilasP

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Viewing my post, I take it I get a happy face for the : followed directly by a p. Sorry bout that
    SilasP

  14. #14
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The placement of the bracket doesn't matter.

    class A { };

    and

    class A
    {

    };

    are the same thing.

    // account.h

    #ifndef ACCOUNT_H_
    #define ACCOUNT_H_

    class accout {
    // body
    };

    #endif // ifndef ACCOUNT_H_

    // sav_acct.h

    #ifndef SAV_ACCT_H_
    #define SAV_ACCT_H_

    #include "account.h"

    class sav_acct: public account {
    // body
    };

    #endif // ifndef SAV_ACCT_H_
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Thanks a lot. That clears all of it up.
    SilasP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  3. Animated GIF Encoding Library
    By Tonto in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2008, 12:57 PM
  4. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  5. Replies: 3
    Last Post: 05-13-2007, 08:55 AM

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