Thread: Header Files

  1. #1
    Unregistered
    Guest

    Header Files

    I was just reading a c++ tutorial and it said that you can make a .cpp file (cplus.cpp for example) save it, then create and new .cpp file and include cplus.h. can you use your own file with smalls bits of data like a class and turn it into a header file?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Pretty simple. Say I want to have a class Foo in a header file. It may look something like this:

    Code:
    // If this header file isn't already included....
    #ifndef FOO_H
    
    // Define the constant FOO_H so it won't be
    //   included more than once...
    #define FOO_H
    
    // Your definitions go here!!
    class Foo 
    {
      private:
        int m_x;
        int m_y;
      public:
        Foo();
        ~Foo();
    };
    
    // This is the end of our definitions
    #endif
    Probably not explained well, but whenever you need that class Foo, just include "foo.h" or whatever you saved it as. Make sure you have the .h extension also. Have any questions just ask. Also where i defined FOO_H , that can be whatever you want really.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Unregistered
    Guest
    whats #ifndef and would i save this file .h?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Unregistered
    whats #ifndef and would i save this file .h?
    1) #ifndef is a preprocessor directive. It simply says, If this is not defined, continue. You have FOO_H after that so it checks to see if another one of your cpp files already included the header. You don't want to include the same stuff twice..

    2) Yes, you would save this as a .h file.

  5. #5
    Unregistered
    Guest
    thank you

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    Ok. I have created some .h file, it contains declarations. But where can I put definitions? They must be in some .cpp file, I think. For example function bodies will be in that .cpp. And how can I tell compiler that I want to use this .cpp always when I am including my .h file?

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Headerfile.h
    #ifndef UNIQUENAME
    #define UNIQUENAME
    class whatever
    {
    //User class declarations and inline methods
    }
    #endif


    Sourcefile.cpp
    #include Headerfile.h
    #include<iostream>
    using namespace std;

    void mymethod::whatever (int something)
    {
    //method definitions
    }


    Main.cpp
    #include Headerfile.h
    #include <iostream>
    using namespace std;
    int main()
    {
    //The program
    }

    It goes something like that. If I made some error than correct it, but you get the picture. I have not used C++ in about 8 months, but I remember it something like this.

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