Hi,

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

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....:confused:

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....

Recommended Answers

All 3 Replies

Hi,

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

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....:confused:

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,

//foo.h

class MyClass
{
    int blah;
public:
    MyClass() : blah(0) {}
    int get_blah();
}
//foo.cpp
#include "foo.h"

int MyClass::get_blah()
{
    return blah;
}

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)

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  #ifndef  __Person_H  
2  #define  __Person_H  
3  
4  
5  class Person{ 
6                       ....
7 };
8 #endif

why use these directives?what do they mean?

also

file: person.cpp

1  #include"person.h"   
2    
3  #include <string.h>
4
5       .....
//implementation of class...

file: main.cpp

1  #include <iostream>  
2  #include "person.h"  
3 using namespace std;   
4  
5  int main(){  
6  .....
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

>>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.