Thread: Classes and header files

  1. #1
    Unregistered
    Guest

    Classes and header files

    I recently purchased SAMS Teach Yourself C++ In 21 Days and it says that i should put classes in a header(.h or .hpp) file but have the class functions in a .cpp file, why is this? and wouldn't it be easier to write the functions in the header file along with the class?

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    This is to hide the implementation of the class. For the user of the class, all he would need to know is how to use the class from looking at the header definition only. This way, commercial applications can give the user the user interface only while keeping the implementation proprietary.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Unregistered
    Guest
    I'm not sure I understand, would you be able to explain that in simple terms please? Also, how could the user look at the header files if the program is compiled? (unless I gave out the source code).

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Search for math.h on your hard drive. When you open it up you'll be able to see all the functions you are able to use but not how they are implemented. It prevents people from nicking code and passing it off as their own
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files and classes
    By disruptivetech in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2008, 09:02 AM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Learning how to use classes and create header files
    By Welshy in forum C++ Programming
    Replies: 10
    Last Post: 04-19-2005, 12:33 PM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM