separating headers from implementation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

separating headers from implementation

 
0
  #1
Nov 8th, 2006
Hi,

i would like to separate the declaration of a class from the definition.
i know about the

  1. void aclass::a_method(){ ..... }

but i dont know anything about what should be written in the header file, and how can i use it through main....

sorry if am not very specific but i don't how to express it...
Can anyone suggest a link for further reading...or maybe explain with an example....

thanks in advance....
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 489
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: separating headers from implementation

 
0
  #2
Nov 8th, 2006
Originally Posted by n.aggel View Post
Hi,

i would like to separate the declaration of a class from the definition.
i know about the

  1. void aclass::a_method(){ ..... }

but i dont know anything about what should be written in the header file, and how can i use it through main....

sorry if am not very specific but i don't how to express it...
Can anyone suggest a link for further reading...or maybe explain with an example....

thanks in advance....
In a header file (generally, a file with an extention of .h or .hpp), the most common approach is to declare classes/functions/etc without providing a definition. Then with a .cpp file of the same name, you provide the definition. eg,
  1. //foo.h
  2.  
  3. class MyClass
  4. {
  5. int blah;
  6. public:
  7. MyClass() : blah(0) {}
  8. int get_blah();
  9. }
  1. //foo.cpp
  2. #include "foo.h"
  3.  
  4. int MyClass::get_blah()
  5. {
  6. return blah;
  7. }
You can #include the into your code header the same way as any other library or header file - making sure you provide its path if the header is inside a different directory.

Edit: The above is somewhat incomplete - you should include header guards to stop the file being included twice, ie,
//foo.h
#ifndef FOO_H
#define FOO_H

class MyClass
{
    int blah;
public:
    MyClass() : blah(0) {}
    int get_blah();
} 

#endif 
The 3 lines highlighted in purple take advantage of the preprocessor, to ensure that everything inbetween the #ifndef and #endif directives can only ever be included once (to include the header file more than once will almost certainly cause a compiler error)
Last edited by Bench; Nov 8th, 2006 at 2:55 pm. Reason: Correction to code snippet
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: separating headers from implementation

 
0
  #3
Nov 8th, 2006
hi, thank you for your answer.
I will try to be more specific, in the book i currently read i see:

file: person.h
  1.  
  2. 1 #ifndef __Person_H
  3. 2 #define __Person_H
  4. 3
  5. 4
  6. 5 class Person{
  7. 6 ....
  8. 7 };
  9. 8 #endif
why use these directives?what do they mean?

also

file: person.cpp
  1.  
  2. 1 #include"person.h"
  3. 2
  4. 3 #include <string.h>
  5. 4
  6. 5 .....
  7. //implementation of class...

file: main.cpp
  1.  
  2. 1 #include <iostream>
  3. 2 #include "person.h"
  4. 3 using namespace std;
  5. 4
  6. 5 int main(){
  7. 6 .....
  8. 7 }
why in main i include only the header file?how does the compiler know where to find find the implementation file....how does it know which implementation file to choose{if i have a lot of files}?
it would seem more logical to include the person.cpp file that has a command to include person.h....

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,455
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: separating headers from implementation

 
0
  #4
Nov 8th, 2006
>>why use these directives?what do they mean?
what directives? on lines 1, 2 and 8? those are called "guard tags" to prevent duplicate declarations if the header file is included more than once. The preprocessor will just ignore the file if it has already been processed. Most of the header files you see nowdays use that technique -- many examples are in the header files installed with your compiler.


>>why in main i include only the header file?how does the compiler know where to find find the implementation file
When you have a project with more than one file you have to tell the compiler's linker what object files have to be included to make the final program. The final executable is made up of either object files (with *.obj files in MS-Windows or *.o in *nix) and the libraries.

If you are using an IDE in MS-Windows environment, such as Dev-C++ or VC++ 2005, then you initially create a project which will contain all the *.cpp and *.h files needed to build the program. And you also have to tell it which libraries to include.

>>it would seem more logical to include the person.cpp file that has a command to include person.h....
that is only for human use, the compiler does not care what *.cpp file(s) you put the implementation code in. And you can have the implementation code in more than one *.cpp file. There is no restriction how you put these together.

main() also needs to include person.h if it wants to instantiate an object of Person class.
Last edited by Ancient Dragon; Nov 8th, 2006 at 3:29 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC