473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

about header files

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
2 987
Meetee
931 Expert Mod 512MB
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
9,208 Expert Mod 8TB
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

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

Similar topics

2
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there...
3
by: dharmesh Gupta | last post by:
Hi All, i am new to header files , and have a basic question , i have made my program in three files namely, idr_bpl.cpp( contains class definitions and function definitions) bpl.cpp(...
2
by: Kevin Joplin | last post by:
Hi there, I've almost finished writing simple server application and i have one little doubt. Suppose we have main server code in server.c file. Rest of the code we divided into some pices and put...
13
by: tsoukase | last post by:
Hello, two questions: 1) Why is a library a collection of compiled source-files while each header-file is a single source? Would it be more efficient if they were both either compiled or not?...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
8
by: Bert | last post by:
Hello, Where can I find good information about who to interpret information from headers of liberaries? Thanks a lot.
10
by: parag_paul | last post by:
HI all, Suppose I have a huge code base, And I have a very common header file. Now there are mulitple places where this header file is kept. This header file cannot be factored as it has...
4
by: mdh | last post by:
K&R briefly mention splitting programs into seperate files.(page 82) Using my compiler (X-code) I noticed that creating a header file gives, as expected one .h file, but creating a new ".c" file...
3
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.