| | |
separating headers from implementation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi,
i would like to separate the declaration of a class from the definition.
i know about the
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....
i would like to separate the declaration of a class from the definition.
i know about the
C++ Syntax (Toggle Plain Text)
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....
•
•
•
•
Hi,
i would like to separate the declaration of a class from the definition.
i know about the
C++ Syntax (Toggle Plain Text)
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....
CPP Syntax (Toggle Plain Text)
//foo.h class MyClass { int blah; public: MyClass() : blah(0) {} int get_blah(); }
CPP Syntax (Toggle Plain Text)
//foo.cpp #include "foo.h" int MyClass::get_blah() { return blah; }
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
Last edited by Bench; Nov 8th, 2006 at 2:55 pm. Reason: Correction to code snippet
¿umop apisdn upside down? hi, thank you for your answer.
I will try to be more specific, in the book i currently read i see:
file: person.h
why use these directives?what do they mean?
also
file: person.cpp
file: main.cpp
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
I will try to be more specific, in the book i currently read i see:
file: person.h
c++ Syntax (Toggle Plain Text)
1 #ifndef __Person_H 2 #define __Person_H 3 4 5 class Person{ 6 .... 7 }; 8 #endif
also
file: person.cpp
c++ Syntax (Toggle Plain Text)
1 #include"person.h" 2 3 #include <string.h> 4 5 ..... //implementation of class...
file: main.cpp
c++ Syntax (Toggle Plain Text)
1 #include <iostream> 2 #include "person.h" 3 using namespace std; 4 5 int main(){ 6 ..... 7 }
it would seem more logical to include the person.cpp file that has a command to include person.h....
thanks
>>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.
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.
![]() |
Similar Threads
- Your code exhibits undefined and implementation-defined behavior (C++)
- error C2447: missing function header(old-style formal list?) (C++)
- Javascript implementation question (JavaScript / DHTML / AJAX)
- Classes and header files (C)
- CA Etrust Audit Software Implementation (Windows NT / 2000 / XP)
- CA Etrust Audit Software Implementation (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Collision Detection
- Next Thread: a simple question{dynamically create object}
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






