Program With C++ style

sdmahapatra's Avatar, Join Date: Jun 2009
Go4Expert Member
Quote:
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
Code:
 .h extension
be present into the header file and
Code:
 .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
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The point of header files is to share definitions between multiple source files. So if you only have one source file there's no point in having a header file.
0
sdmahapatra's Avatar, Join Date: Jun 2009
Go4Expert Member
Thanks for your suggestion, So xpi0t0s, If I don't have one source file then how to do this? header files,class,member functions(for process) and main(for application) should be in separate window and I want to execute the program properly.

Thanks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It doesn't necessarily follow that stuff that has to be in different windows has to be in different source files. Form design and software design are linked but do not necessarily have to follow each other. Anyway here is an example of a header file included by two source files (not tested):

inc.h:
Code:
#define FOO 27
int bar();
main.c:
Code:
#include <stdio.h>
#include "inc.h"

int main()
{
  printf("FOO=%d; bar()=%d\n",FOO,bar());
  return 0;
}
gronk.c:
Code:
#include <stdio.h>
#include "inc.h"

int bar()
{
  printf("in bar(), returning %d\n",FOO);
  return FOO;
}
Compile with something like gcc main.c gronk.c -o main
and this will compile main.c and gronk.c to produce main.o and gronk.o, then link them together.
Output should be something like:

in bar(), returning 27
FOO=27; bar()=27
0
sdmahapatra's Avatar, Join Date: Jun 2009
Go4Expert Member
Hi xpi0t0s, yes I'll try it and if it'll going well then using this way I'll try to go more depth and then I let you know. thanks xpi0t0s for your concern about this problem.I think it will enough for me.thanks