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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
that , when printf() is called in c , it is also situated in <stdio>
and it is a header file , so there is only declaration of printf() , but after declaration checking by compiler , from where
the defination of printf() came ?
There are two, unrelated issues here. I will speak, first to one, then to the other.
"Header files" contain only the definition of data structures and other things which pieces of software that need to use or to refer to some other part of the same or a different program need to know in order to do so. The implementation of those things will be found in different files, which may or may not even be part of the same project. (For example, if you're referring to a file-compression library, your program needs to "know the particulars of it" ... which it gets by means of the header files provided by the library's authors. But you're not compiling that library at all.)
----
"Classes" are a way to compartmentalize the various pieces of your program's logical structure ... no matter what file-structure is used to contain the source code. The use of classes may enable your program to consist of more-or-less self contained components which contain, in one "thing," both data and the associated logic needed to work with and to interpret that data. Classes may also enable you to describe common functionality "only once" and to be able to define variations to that common functionality by describing and implementing only the differences.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
the include directive tells the compilerwhich h files to look in when the function, struct or class definition is not in the cpp file it's compiling. If you try to compile a program which uses a function from stdio it will not compile unless you tell the compiler that it should include stdio.
I'm guessing printf is probably the same whether you're writing C or C++ and the header files probably contain the same code regardless of the language. C++ is a superset of C, meaning it contains all the functionality of C plus some more -- you can write pure C code and compile it with C++ compiler and you'll not use any objects (in theory at least). But in C++ you can also use objects not contained in C's libraries and create your own objects.
C++ is only "better" (if you like object-orientation) than C if you use it that way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.