views:

66

answers:

1

In C++, what's the benefit of having a class with functions...

say

class someClass{
 public:
  void someFunc(int arg1);
};

then having the function's actual functionality declared after int main

int main() 
    { return 0; }

void someClass::someFunc(int arg1)
    {   cout<<arg1; }

Furthermore, what's the benefit of declaring the class in a .h header file, then putting the functionality in a .cpp file that #includes the .h file?

+6  A: 

Dependency management. Users of the class only need to include the header file, so they don't depend on the implementation.

Another use is breaking circular dependencies.

Both issues may look like a waste of time with toy programs, but they start to grow into a really bad problem as the program grows.

Péter Török
For an example see http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c
SDX2000
Dependency management even becomes critical for programs spanning a few hundreds of thousands of lines. You don't want to recompile the whole thing when someone changes a comment :/
Matthieu M.