开发者

What's the benefit of declaring class functions separately from their actual functionality?

开发者 https://www.devze.com 2023-01-01 07:10 出处:网络
In C++, what\'s the benefit of having a class with functions... say class开发者_如何学JAVA someClass{

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

say

class开发者_如何学JAVA 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消