I currently have a project with 3 files.
One DBheader.h header file that includes:
- Class declarations (with their smaller member function definitions)
A DBdefinitions.cpp file with:
- Larger member function definitions for the classes in
DBheader.h
and finally a DBmain.cpp file that contains:
- Main code
- Some large (non-memeber) functions that use the classes defined in
DBheader.h
I would preferably like to move these functions somewhere to make my DBmain.cpp file less cluttered. Should/could I move them to the DBdefinition.cpp file or do I need to create a new separate .cpp file for non-member functions?
Here’s a rough of what my code looks like if the above is unclear.
//DBheader.h
//libraries..
class course{
//member data..
void printinfo();
}
–
//DBdefinitions.cpp
#include "DBheader.h"
void course::printinfo(){/*do stuff*/}
–
//DBmain.cpp
#include "DBheader.h"
typedef map<int,course> record;
void fileinput(record &map);
int main(){
//stuff
}
void fileinput(record &map){
//lots of code
}
You should organize free functions that are part of the public interface in a similar fashion you used for the
classdefinition: a header with the declaration and an implementation file with the implementations.If they are particular to a specific translation unit, keep them in that implementation file.
All free functions should be declared inside a
namespace(named for the public ones, anonymous for “private” free functions).