LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 09-05-2007, 06:00 PM   #1
Ryupower
Member
 
Registered: Oct 2006
Posts: 65
Thanked: 0
What's the Difference between Header files and a Class?


[Log in to get rid of this advertisement]
Just a stupid c++ question I have since I'm a nub.
Ryupower is offline  
Tag This Post , , , ,
Reply With Quote
Old 09-05-2007, 06:13 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Thanked: 0
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.
wjevans_7d1@yahoo.co is offline     Reply With Quote
Old 09-05-2007, 07:51 PM   #3
paulsm4
Senior Member
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 3,316
Thanked: 144
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!
paulsm4 is offline     Reply With Quote
Old 09-05-2007, 10:27 PM   #4
Ryupower
Member
 
Registered: Oct 2006
Posts: 65
Thanked: 0

Original Poster
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.
Ryupower is offline     Reply With Quote
Old 09-05-2007, 11:50 PM   #5
paulsm4
Senior Member
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 3,316
Thanked: 144
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
paulsm4 is offline     Reply With Quote
Old 09-06-2007, 10:44 PM   #6
Ryupower
Member
 
Registered: Oct 2006
Posts: 65
Thanked: 0

Original Poster
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?
Ryupower is offline     Reply With Quote
Old 09-07-2007, 01:30 AM   #7
Nylex
HCL Maintainer
 
Registered: Jul 2003
Distribution: Slackware
Posts: 5,783
Thanked: 40
I have trouble understanding what you're asking :/. Classes and header files are two different things..
Nylex is offline     Reply With Quote
Old 09-07-2007, 12:21 PM   #8
paulsm4
Senior Member
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 3,316
Thanked: 144
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 12:23 PM..
paulsm4 is offline     Reply With Quote
Old 09-07-2007, 03:56 PM   #9
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Thanked: 0
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.
wjevans_7d1@yahoo.co is offline     Reply With Quote
Old 09-09-2007, 01:55 AM   #10
Ryupower
Member
 
Registered: Oct 2006
Posts: 65
Thanked: 0

Original Poster
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 02:01 AM..
Ryupower is offline     Reply With Quote
Old 09-11-2007, 08:59 AM   #11
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Thanked: 0
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.
wjevans_7d1@yahoo.co is offline     Reply With Quote
Old 09-12-2007, 10:18 PM   #12
Ryupower
Member
 
Registered: Oct 2006
Posts: 65
Thanked: 0

Original Poster
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.
Ryupower is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


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


All times are GMT -5. The time now is 06:36 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration