how to create a .cpp and .h file

Pages: 12
Apr 13, 2009 at 7:06pm
Hello , I am new c plus plus student. I read this tutorial and it looks a good one.I will be glad if someone tells me how to create a .cpp and .h files for a program.let suppose for this program.


#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}
Apr 13, 2009 at 8:13pm
Do you know which compiler or environment you are using?
Apr 13, 2009 at 8:43pm
microsoft visual c++ express edition
Apr 13, 2009 at 8:58pm
File > New > File... > Visual C++ > C++ File (.cpp)

-If you want to create a program you should create a project first:
File > New > Project > (The template you want) [I suggest you the "Empty Project"]
Then you can add files:
Project > Add New Item...
Last edited on Apr 13, 2009 at 8:59pm
Apr 13, 2009 at 9:00pm
i made .cpp file and compile the program by doing all that.
now how can i create .h file ?
Apr 13, 2009 at 9:05pm
The same way you create a cpp:
(to add a header file to a project)
Project > Add New Item... > Header File (.h)
Then you can #include "yourHeader.h" in other files in your project
Last edited on Apr 13, 2009 at 9:05pm
Apr 13, 2009 at 9:17pm

let say the program name is program1.h
so will the following program work

#include <iostream>
#include"program1.h"
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}
Last edited on Apr 13, 2009 at 9:18pm
Apr 13, 2009 at 9:24pm
Yes

(the header name should be "program1.h", the name of the program doesn't matter)
Apr 13, 2009 at 9:37pm
if this will work then when do we use this

#ifndef _YourHeader
#define _YourHeader

#endif


Is this another way of making .h file ?

Apr 13, 2009 at 9:43pm
if this will work then when do we use this

#ifndef _YourHeader
#define _YourHeader

#endif
You need include guards in order to be sure that you don't include a header multiple times. It isn't required but without that you can get errors


Is this another way of making .h file ?
You can open notepad (or any other text editor) and save the file with a .h extension
Apr 13, 2009 at 9:55pm
So you mean the header file pragram with .h extension should be like this

#include<iostream>
#include"program1"
#ifndef _YourHeader
#define _YourHeader

#endif
int main()
{
cout<<"hello world";
return 0;
}
Apr 13, 2009 at 10:17pm
no,
header:
1
2
3
4
#ifndef _YourHeader
#define _YourHeader
//code
#endif 

source file:
1
2
3
4
5
6
7
8
#include<iostream>
#include"YourHeader.h"

int main()
{
    std::cout<<"hello world";
    return 0;
}
Apr 13, 2009 at 10:26pm
I am sorry I am very new .what is the difference between header and source file ?
As far as I know , we include headers in the source file and make it workable. Isn't it ?
Apr 13, 2009 at 10:43pm
In the header files you have declarations so you can use function, classes, whatever which are not defined in your source file (they can be defined in another source file you will compile or in a library, for template stuff, you will have the definition in the header)

having the code in multiple source files can be useful for large projects, here is an example of using headers for having the code split in two source files:

MyHeader.h
1
2
3
4
5
6
#ifndef MYHEADER
#define MYHEADER

    void sayHello();//declaration

#endif 

MyHeader.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "MyHeader.h"

void sayHello()//Implementation
{
    std::cout << "Hello!";
}

Main.cpp
1
2
3
4
5
6
7
#include "MyHeader.h"

int main()
{
    sayHello();//Call
    return 0;
}
Apr 13, 2009 at 10:57pm
I suggest you try Dev C++.
Apr 13, 2009 at 11:04pm
if i compile MyHeader.h

#ifndef MYHEADER
#define MYHEADER

void sayHello();//declaration

#endif

and try to get the result on the console will it work ?


what is the difference between main.cpp and myheader.cpp ?

Last edited on Apr 13, 2009 at 11:04pm
Apr 14, 2009 at 8:24am
headers don't get compiled, you should compile myheader.cpp, main.cpp and link them (If you are using VC++ IDE and these files are in the same project you just need to build the project)
main.cpp is the main file (with the program entry point)
myheader.cpp is the file with the body of the function declared in myheader.h
Apr 14, 2009 at 7:33pm
How can I know that the header file is good and working.Like for .cpp file I can compile and check it but you said we cannot compile .h files. so how can I know that it has linked with .ccp file.
Apr 14, 2009 at 8:41pm
Which compiler are you using?
Apr 14, 2009 at 9:00pm
microsoft visual c++ 2008 express edition
Last edited on Apr 14, 2009 at 9:35pm
Pages: 12