By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

about header files

P: 2
i have created a project for demonstration of self defined header files

I have put the definition of class in header file named aaa.h

then all the definitions of member functions and constructors, i put in aaa.cpp

Now i have created a file called aaamain.cpp in which I have included aaa.h and then called main()

now i am confused how to make link b/w files aaa.h and aaa.cpp(in which all definitions are stored)

Pls tell me the right way to use header files
Aug 13 '07 #1
Share this Question
Share on Google+
2 Replies


Meetee
Expert Mod 100+
P: 931
i have created a project for demonstration of self defined header files

I have put the definition of class in header file named aaa.h

then all the definitions of member functions and constructors, i put in aaa.cpp

Now i have created a file called aaamain.cpp in which I have included aaa.h and then called main()

now i am confused how to make link b/w files aaa.h and aaa.cpp(in which all definitions are stored)

Pls tell me the right way to use header files
Outside main you will have to define functions: For eg.

in .h file you have wriiten
Expand|Select|Wrap|Line Numbers
  1. class Hello
  2. {
  3. public:
  4. void fun();
  5. };
then in .cpp
Expand|Select|Wrap|Line Numbers
  1. void Hello::fun()
  2. {
  3. ..define function...
  4. }
  5. int main()
  6. {
  7. Hello obj;
  8. obj.fun();
  9. return 0;
  10. }
  11.  
This is a general structure, you can use it in your project accordingly.

Regards
Aug 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not correct:
void Hello::fun()
{
..define function...
}
int main()
{
Hello obj;
obj.fun();
return 0;
}
By having the class member functions in rhe same file as main() makes reuse of the class member functions impossible unless you also take the main() to the next program. That gives two main() functions and the build dies in the link. By making copies of the code, you can reuse the code the but you now have two copies to maintain. Maybe more if a third program becomes involved.

What should happen is:

In aaamain.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <aaa.h>
  2. int main()
  3. {
  4. ....etc..
  5. }
  6.  
In aaa.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <aaa.h>
  2. int class::Method()
  3. {
  4. ....etc..
  5. }
  6.  
Then both aaamain.cpp and aaa.cpp are added to the same project. When the project is built, both files are compiled and the object files are moved by the liunked to the executable. The linker's job is to connect those function calls in main() with the code in aaa.cpp. You don't have to worry about that.
Aug 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.