0

Possible Duplicate:
In C++ why have header files and cpp files?

I was reading some KDE tutorials for creating basic plasma widgets and other QT stuff. One thing that I noticed is almost all the programs has a .h and .cpp with header file included in the cpp file.

The basic thing that I get is to minimize the clutter in the file and make the code more readable.

My question is what things should I put in a .h file and .cpp file writing a program and how it is going to benefit me in improving my code.

For example - I've created 3 files , add.h, add.cpp and pass.cpp - Now add.h has the function while other 2 have definition.

Add.h

#include <iostream>

using namespace std;

int add (int x,int y);

Add.cpp

#include "add.h"

int main() {

    add (int x, int y) {

    int z = x+y;

    cout<<"Add is "<<"\t"<<z;

    }
return 0;
}

Pass.cpp

#include "add.h"


add (3,4);

However this doesn't work , judging from the answer here Why have header files and .cpp files in C++?

contents of add.h are automatically copied yet I get several errors.

Community
  • 1
  • 1
Shashwat
  • 269
  • 1
  • 3
  • 9

3 Answers3

3

Main reason is that you can include the .h from multiple .cpp files to promote sharing of code across modules/classes.

For a very simple app (e.g. "hello world", then it's true. Splitting into .cpp and .h doesn't have a lot of benefit)

John3136
  • 28,809
  • 4
  • 51
  • 69
2

You regularly need to access the things you define in a .cpp file from other translation units - those other translation units need to know what they can access, so those things need to be declared in the other translation unit. Putting the declarations in a header file means that you can just include them in the other translation unit, rather than duplicating them in every translation unit that needs access to the declared elements.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
2

Write all the functions completely in your ".cpp" file and write it first of all in your program. In your ".h" file, write just the proptype, that is the declarations of your functions. Make this ".h" file at the end of your program. And don't forget to write a semi-colon(;) at the end of each function prototype.

This is a good programming practice and it gives you the benefit of writing the code once in these separate files and using their code across your entire project or any other number of files.

Suppose you have a file "myProgram.cpp" and myProgram.h, you can just include this one line at the beginning of any program to use its code. #include "myProgram.cpp".

It works well when you have a number of functions and declarations in your file. If you have a small piece of code, there is no need of creating these separate files.

Sasha
  • 492
  • 2
  • 6
  • 21
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
  • SO means declaration part happens in .h and body part in .cpp. What if I create a a.h file with method int add(int x,int y); and then include the same method in 2 cpp files. Ho will I be able to control the final output (at compile time) consider one cpp file pass the argument to function and other add it. – Shashwat Sep 03 '12 at 03:37
  • You can define the a function only once. If you do as you are saying, compiler will give you multiple definition error...!!! – Coding Mash Sep 03 '12 at 05:33
  • What I meant is defining the add function in header file and then creating multiple defination of the function,as - in b.cpp I am passing argument to the function and in c.cpp I am creating the main body function and doing all the addition using add function. So in that case how I make sure that b.cpp is called first and then c.cpp so that parsing of data happens correctly ? – Shashwat Sep 03 '12 at 08:11
  • I've edited the main post with my question . Please do look @codingmash – Shashwat Sep 03 '12 at 09:12
  • Well you are declaring the function in the wrong way. I can't show it to you here in the comments. If you come here, i shall be quite happy to assist you with the syntax. https://www.facebook.com/codingmash – Coding Mash Sep 03 '12 at 11:16