You are here:

C++/C++ header files

Advertisement


Question
I have knowledge on how to program in java and have been trying to learn c++ over the internet. when i came across header files in c++ i got confused in how to properly create them since you dont create them in java. From my understanding, they are used as an interface to show other classes that use the class what variables and methods are availble for them to use, but im confused in whether you have to declare variables and methods that are private in the header file, since other classes cant use them.

Answer
Hi, Jeremy.

A header file is basically just a way of splitting (mostly) the declarations from the implementations (there are exceptions, hence my mostly parenthetical).  You will declare a class and all of its functions and variables in a header file, even private ones.

Don't think of private functions and variables as strictly private.  There are circumstances where protected and private functions and variables can actually be accessed.  Friend classes can access both private and protected items, and child classes can access protected items (but not private).

I mentioned earlier that there are exceptions to the rule of "declare in header, implement in CPP."  These exceptions come in a couple of forms.  One is inline functions.  Inline functions are generally implemented right in the header file.  They are (or should be) small functions, only a line or two, so it generally doesn't clutter the file too much.  Many accessors, even non-inline ones, are often written directly in the header, as well.  The other exception is template classes (which you can worry about when you're further along in your C++ studies).  Template classes must be entirely implemented within the header, unless a particular keyword is supported; a keyword which I cannot even remember at the moment because it is so rarely supported... just assume that a template is always implemented in the header with no CPP file allowed.

I hope this helps clear up your confusion about header files.  Coming from Java to C++ is not quite as easy as from C++ to Java, I think.  At least, that's my opinion.  The two are extremely similar, though, so I don't suspect you will have much difficulty.  I wish you luck in your studies and I am always here to answer any questions you may have.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.