Hi all,I have written a program in c++ using visual studio 2008.I've create only one source file and written all the codes including header files within .cpp extension and program is running well.But I want to split this program like

.h extension

be present into the header file and

.cpp extension

into the source files separately (one is for functions and one is for main()) and then want to execute the whole program.

I've tried myself but failed as I never before use visual studio (VC++).So I need your suggestion to solved this problem.Which steps I need to follow?How do I separate the module in proper way?Which portion should I care more?etc....So please give your suggestion to know actual advantages of c++/vc++.


With warm Regards
Sdmahapatra

Why are you creating two threads for the same purpose....?
This thread is just the same...

And with regard to your question you can create a new file say headerfile.h with .h extension and place it in your working directory.Then by usage of #include "headerfile.h" above your main() source code in your cpp file and work.

Thanks for your suggestion csurfer.

Why are you creating two threads for the same purpose....?

In future to implement this current small project that's why I need to go this way.I tried what you have suggest but csurfer, I can't and I need more precise way.he main() function should be use for applications.At the main() .cpp window no other function should be there and from main() window I want to call others function.I don't want to put all header files in every source file.So tell me csurfer, Which way should I go? #pragma directive or any others way?
Thanks

If I don't have one source file then how to do this? Actually I need that the header files,class,member functions(for process) and main(for application) should be in separate window and I want to execute the program properly.

If I don't have one source file then how to do this? Actually I need that the header files,class,member functions(for process) and main(for application) should be in separate window and I want to execute the program properly.

To add another *.c, *.cpp or *.h file to the project, select the New Item as shown in the first bitmap I attached to this post. That will bring up the screen shown in the second bitmap picture. In that screen just select "Code" and fill in the other stuff.

It is standard practice to create one or more header files (with *.h extension) then use the #include "MyHeader.h" at the top of each *.cpp file. Projects can have as many *.h and *.cpp files as you want, but only one of them can contain main() function. If you have to share global variables among the *.cpp files then att their declaration to the *.h file with the extern keyword, and declare them again in the *.cpp file without extern.

An example of this might be

// header file
#ifndef MYHEADER_H // code guards
#define MYHEADER_H

int foo(int x, int y); // function prototype

extern int MyGlobal; 
#endif // end of code guards
// cpp file
#include "MyHeader.h"

// globals
int MyGlobal = 0;

int foo(int x, int y)
{
    return x * y;
}

Hi Ancient Dragon,this is exactly what I looking for.thanks a lot friend.Using this way I can go more deeply.thanks I'll keep touch with you

Hi csurfer, thanks for your cooperate friend.Ancient Dragon has give also a valuable suggestion which is beneficial for me.Let me try and let you know.

Will anybody tell me whats are the difference between

code guards and #pragma once.

they are doing almost same task then which one will properly use for which purpose?

With Warm Regards
sdmahapatra

Code guards will work everywhere, but #pragma once will only work if the compiler supports it. What's worse, one compiler might define #pragma once to do the same thing as code guards and another compiler might define it to do something completely different. Your code would break on the second compiler.

If you have the option of choosing between two constructs with the same behavior, choose the one that is more portable.

OK,it's mean that generally use of Code guards is less risk than #pragma once .Thanks you Tom Gunn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.