Creating a header file / managing multiple cpp's

Ok this one is probably really simple but all example of header files I have found seem to have all the functions built into the header itself.

Example scenario:

I have a project that has my normal main.cpp which contains the main function. I have 10 huge functions that are all 1,000+ lines of code, I want to group these together and "hide" them without having to prototype them all. I am assuming I need to create a header file for this.

I would like to have each function in its own CPP (a.cpp , b.cpp , c.cpp etc) to make it really neat and easier to edit. So I would like to create a header that simply prototypes each cpp and then my project acknowledges these functions.

I am using Visual Studio should that make any difference is there a really easy way to group/manage+hide data?

Cheers,

Ben.
You just need to declare the functions in the header file and define them inside the .cpp files. For example:

MyProg.h:
1
2
int func1();
int func2(int, double);


func1.cpp:
1
2
3
4
5
int func1()
{
   // do some stuff
   return 0;
}


func2.cpp:
1
2
3
4
5
int func2(int i, double d)
{
    // do some stuff with i and d
    return 7;
}


I have 10 huge functions that are all 1,000+ lines of code

A program that requires thousands of lines of code is not a beginner task. On the other hand, if you're a beginner and writing a program with thousands of lines, chances are good that your task can be solved with fewer lines. You may want to post some of the code to see if anyone has ideas of ways to make it shorter.
Thanks for the quick reply, I was just using a really basic example :). So how do I then link "MyProg.h" with those cpp's? so if I was to create an entirely new project but I wanted the use of func1 and func2 , how do I make it so the header file is always linked to those cpps?
There is no way to 'link' MyProg.h to your source files. Remember, all it is doing is just prototyping the functions - until the linker runs, there is no way for it to know that those functions actually exist.

Also, just a few things regarding your example. First, having a single function being 1000+ lines of code is just silly - often people have rules like "functions shouldn't be longer than 50 lines" or the like. Also, don't just throw each function into a different source file by default - put related functions into the same source file. It makes it easier to make sense of.
Each of your .cpp files should have #include "MyProg.h" near the top. That way each cpp file knows the prototypes of the functions.
Topic archived. No new replies allowed.