how to create a .cpp and .h file

Pages: 12
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;
}
Do you know which compiler or environment you are using?
microsoft visual c++ express edition
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
i made .cpp file and compile the program by doing all that.
now how can i create .h file ?
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

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
Yes

(the header name should be "program1.h", the name of the program doesn't matter)
if this will work then when do we use this

#ifndef _YourHeader
#define _YourHeader

#endif


Is this another way of making .h file ?

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
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;
}
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;
}
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 ?
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;
}
I suggest you try Dev C++.
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
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
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.
Which compiler are you using?
microsoft visual c++ 2008 express edition
Last edited on
Pages: 12