LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-05-2007, 05:00 PM   #1
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Rep: Reputation: 16
What's the Difference between Header files and a Class?


Just a stupid c++ question I have since I'm a nub.
 
Old 09-05-2007, 05:13 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.

Get thee to google and google thee this:

Code:
C++ tutorial
Hope this helps.
 
Old 09-05-2007, 06:51 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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.

'Hope that helps .. PSM

PS:
Not a stupid question at all, really!
 
1 members found this post helpful.
Old 09-05-2007, 09:27 PM   #4
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
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?

That's where I get confused.
 
Old 09-05-2007, 10:50 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 09-06-2007, 09:44 PM   #6
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by paulsm4 View Post
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?
 
Old 09-07-2007, 12:30 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I have trouble understanding what you're asking :/. Classes and header files are two different things..
 
Old 09-07-2007, 11:21 AM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I'm with Nylex - I'm not at all sure what your question is.

Please look here for a good explanation of a "header file":
http://en.wikipedia.org/wiki/Header_file

After that, please read this:
http://www.gamedev.net/reference/pro...ures/orgfiles/

Please post back with any specific questions you might still have.

Thanx in advance .. PSM

Last edited by paulsm4; 09-07-2007 at 11:23 AM.
 
Old 09-07-2007, 02:56 PM   #9
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Quoth Ryupower:
Quote:
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.

Hope this helps.
 
Old 09-09-2007, 12:55 AM   #10
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by wjevans_7d1@yahoo.co View Post
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?

N00b questions like that. Sorry. ^^;

Last edited by Ryupower; 09-09-2007 at 01:01 AM.
 
Old 09-11-2007, 07:59 AM   #11
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
Old 09-12-2007, 09:18 PM   #12
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by wjevans_7d1@yahoo.co View Post
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.
OK, now we got it. Thanks a lot.
 
1 members found this post helpful.
Old 07-05-2012, 06:54 AM   #13
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
i have a question !

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 ?
 
Old 07-05-2012, 08:36 AM   #14
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,062

Rep: Reputation: 972Reputation: 972Reputation: 972Reputation: 972Reputation: 972Reputation: 972Reputation: 972Reputation: 972
Let me try.

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.
 
1 members found this post helpful.
Old 07-05-2012, 09:37 AM   #15
273
Senior Member
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid + various in VMs.
Posts: 2,866

Rep: Reputation: 633Reputation: 633Reputation: 633Reputation: 633Reputation: 633Reputation: 633
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.
 
  


Reply

Tags
c++, class, cpp, difference, header


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
TOS in IP Header set to Minimize-Dealay - no difference ddaas Linux - Networking 5 07-26-2005 07:32 PM
gcc header files and cpp files? EchO Linux - Software 3 03-01-2005 01:14 AM
Difference between including a header file and 'class' declaration in C++ scoTtkIm Programming 1 09-09-2004 04:23 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM


All times are GMT -5. The time now is 05:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration