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 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. any help with the question would be greatly appreciated.

Answer
Hi Jeremy, thank you for the question.

A header file in C++ is pretty much equivalent to a .java file in Java, except that the definitions of functions are located in a .cpp file most of the time. It's done like this so you can hide your implementation if you so choose. When most people sell their C++ API, they provide the header files and a library, in which the implementation resides. That way users can include your headers so they can call your functions, and then link to the library so the code can be used without them seeing your proprietary implementation. It's kind of like how your import in a java file, and then provide a jar that contains the code.

Java requires that every method and variable be labeled with an access keyword. In C++, you group methods and variables inside access keywords.

Java:

class Foo
{
private int x;
public int getX() { return x;}
}

C++:

class Foo
{
private:
int x;

public:
int GetX() { return x; }
}

In C++ you can have as many private/public/protected sections that you want.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.