What's the Difference between Header files and a Class?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Header files contain any arbitrary C++ code you want. They typically don't contain any function definitions, because they usually are intended to be included by more than one C++ module in the same executable.
A class can be defined in a header file or in a regular (.cpp or .cxx) file. A class is exactly like a struct except that its members are by default private, not public.
Every decent language gives you some way to express "interface" (e.g. your constants, typedefs and class declarations) vs "implementation" (how it actually works).
By convention, C++ .h header files usually contain your "interfaces", and .cpp source files contain your "implementations".
Using this convention (.h vs .cpp) is an easy way to obey what Stroustrup (the guy who invented C++) calls the "One Definition Rule (ODR)" - you must not have multiple definitions ("implementations") of the same object.
I'm a bit confused, but I'll get it out of your info somehow, thanks.
Why then is C++ more useful than C because of classes? Can't header files be used the same way without classes?
Like, C has commands where the information to them is in the header, right?
What makes the class more useful than a command you programmed in there?
I'm sorry - I thought your question was about headers vs source files, not C vs C++. Please let me clarify:
1. What I said above is true: by convention, it's good form to put your "implementation" in .h header files, and your actual "implementation" in source files.
2. This is merely a convention, not a rule. You can do otherwise if you wish (for example, you can put everything into one, big, happy file). But it's seldom a good idea - especially for larger projects or team efforts.
3. This convention is equally true for C and for C++. The only real difference is that in C++. your interface is likely to declare classes :-)
4. To categorically say "C++ is more useful than C" is simply wrong. Both languages are merely tools - some people prefer one tool over another; some tools are intrinsically better for different tasks than another. There is no "better" or "worse"; the real answer is "it depends".
I'm sorry - I thought your question was about headers vs source files, not C vs C++. Please let me clarify:
1. What I said above is true: by convention, it's good form to put your "implementation" in .h header files, and your actual "implementation" in source files.
2. This is merely a convention, not a rule. You can do otherwise if you wish (for example, you can put everything into one, big, happy file). But it's seldom a good idea - especially for larger projects or team efforts.
3. This convention is equally true for C and for C++. The only real difference is that in C++. your interface is likely to declare classes :-)
4. To categorically say "C++ is more useful than C" is simply wrong. Both languages are merely tools - some people prefer one tool over another; some tools are intrinsically better for different tasks than another. There is no "better" or "worse"; the real answer is "it depends".
'Hope that answers your questions .. PSM
Well...they say it's object oriented. But don't commands stored in header files and classes do the same, basically?
don't commands stored in header files and classes do the same, basically?
To begin with, the main difference between a header file and a class is that a header file contains C++ code (but not usually function definitions), and a class is C++ code (of a particular kind).
And to answer your latest comment, we need to be more precise here. What do you mean by "command"? That word has no inherent meaning in C++. The closest thing we have to a "command" is a function definition.
A function declaration might look like this:
Code:
int foo(float bar);
A function definition might look like this:
Code:
int foo(float bar)
{
return (int)(5*bar);
}
Typically, the name of each .cpp (or .cxx) file will appear on a gcc command line. The file will not be #included anywhere. The compiler will see that file only once each time you compile your program. It will contain one or more function definitions.
Typically, the name of each .h file name will not appear on a gcc command line. The file will be #include in one or more of the .cpp files. The compiler see that file once for each time it's #included. It will contain no function definitions, which is a good thing, because you don't want to define the same function more than once.
A class interface usually involves no function definitions (although it often involves function declarations), and is therefore often found in a .h file, where the compiler will typically see it many times.
A class implementation usually involves function definitions, and is therefore usually found in a .cpp file, where the compiler will typically see it only once each time you compile your program.
Quoth Ryupower:To begin with, the main difference between a header file and a class is that a header file contains C++ code (but not usually function definitions), and a class is C++ code (of a particular kind).
And to answer your latest comment, we need to be more precise here. What do you mean by "command"? That word has no inherent meaning in C++. The closest thing we have to a "command" is a function definition.
A function declaration might look like this:
Code:
int foo(float bar);
A function definition might look like this:
Code:
int foo(float bar)
{
return (int)(5*bar);
}
Typically, the name of each .cpp (or .cxx) file will appear on a gcc command line. The file will not be #included anywhere. The compiler will see that file only once each time you compile your program. It will contain one or more function definitions.
Typically, the name of each .h file name will not appear on a gcc command line. The file will be #include in one or more of the .cpp files. The compiler see that file once for each time it's #included. It will contain no function definitions, which is a good thing, because you don't want to define the same function more than once.
A class interface usually involves no function definitions (although it often involves function declarations), and is therefore often found in a .h file, where the compiler will typically see it many times.
A class implementation usually involves function definitions, and is therefore usually found in a .cpp file, where the compiler will typically see it only once each time you compile your program.
Hope this helps.
Yes, I'm talking about things like Cout, Cin, etc... aren't they stored in iostream?
( or was it cstdio? )
Or printf...that stuff.
Aren't they all functions in the header file? What advantage is a class over those?
Header files typically contain no actual code. No function definitions.
They often contain function declarations, which inform the C compiler how your calls to these functions must appear. For example, they might inform the compiler that when you call printf(), the first argument must be a string. (Well, an array of characters. Well, actually, a pointer to the first character of such an array. But I digress. Harrumph.)
After your code is compiled, it is linked with various libraries of other code, previously compiled, usually compiled before you ever got your distribution of Linux. An example would be the code that implements printf().
Same with cin and cout.
You know what a struct is, right? As stated in post #2 of this thread, a class is very similar to a struct.
The difference between a C struct and a C++ struct (or class) is that a C++ struct (and a C++ class) may contain function declarations as members. The struct definitions (including any function declarations) typically go into the header file.
If you define your own classes which contain function declarations, you must also define those functions (write the code for them). You would typically not make those definitions part of the class definition (and would typically not place these function definitions in a header file), but put them in a cpp file.
Header files typically contain no actual code. No function definitions.
They often contain function declarations, which inform the C compiler how your calls to these functions must appear. For example, they might inform the compiler that when you call printf(), the first argument must be a string. (Well, an array of characters. Well, actually, a pointer to the first character of such an array. But I digress. Harrumph.)
After your code is compiled, it is linked with various libraries of other code, previously compiled, usually compiled before you ever got your distribution of Linux. An example would be the code that implements printf().
Same with cin and cout.
You know what a struct is, right? As stated in post #2 of this thread, a class is very similar to a struct.
The difference between a C struct and a C++ struct (or class) is that a C++ struct (and a C++ class) may contain function declarations as members. The struct definitions (including any function declarations) typically go into the header file.
If you define your own classes which contain function declarations, you must also define those functions (write the code for them). You would typically not make those definitions part of the class definition (and would typically not place these function definitions in a header file), but put them in a cpp file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.