944,090 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1646
  • C++ RSS
Nov 8th, 2006
0

separating headers from implementation

Expand Post »
Hi,

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

C++ Syntax (Toggle Plain Text)
  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....
Similar Threads
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Nov 8th, 2006
0

Re: separating headers from implementation

Click to Expand / Collapse  Quote originally posted by n.aggel ...
Hi,

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

C++ Syntax (Toggle Plain Text)
  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,
CPP Syntax (Toggle Plain Text)
  1. //foo.h
  2.  
  3. class MyClass
  4. {
  5. int blah;
  6. public:
  7. MyClass() : blah(0) {}
  8. int get_blah();
  9. }
CPP Syntax (Toggle Plain Text)
  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
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Nov 8th, 2006
0

Re: separating headers from implementation

hi, thank you for your answer.
I will try to be more specific, in the book i currently read i see:

file: person.h
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  1.  
  2. 1 #include"person.h"
  3. 2
  4. 3 #include <string.h>
  5. 4
  6. 5 .....
  7. //implementation of class...

file: main.cpp
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Nov 8th, 2006
0

Re: separating headers from implementation

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Collision Detection
Next Thread in C++ Forum Timeline: a simple question{dynamically create object}





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC