Thread: Header Files

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Header Files

    I'm using Borland v.4.52. How do you make, include and use your own header files?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, here is an example of a basic header file.

    Code:
    // Example.h
    #ifndef  _EXAMPLE_H_ // says, if it isn't defined, define
    #define _EXAMPLE_H_
    
    class cExample {
    private:
         int m_x;
         int m_y;
    public:
         inline int GetX( void ) { return ( m_x ); }
         inline int GetY( void ) { return ( m_y ); }
    };
    
    #endif /* _EXAMPLE_H_ */
    This might seem a little confusing at first but basically this is just a simple class that your main.cpp file can use. Make sure you add the line

    #include "Example.h"

    to your main.cpp in the beginning. Using quotes as opposed to the brackets you are accustomed to means that it is a header file you defined and is located in the directory your source code is located in. Now if I wanted to declare an instance of this class in my main.cpp you would write it like this.

    Code:
    int main( void )
    {
         cExample cEx;
         return ( 0 );
    }
    This would declare an instance of the class and you could access the public members as you normally would. Hopefully this helps you some.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM