What is the difference between a .cpp file and a .h file?

后端 未结 8 2084
余生分开走
余生分开走 2020-12-02 11:52

Because I\'ve made .cpp files then transfered them into .h files, the only difference I can find is that you can\'t #include .cpp files. Is there any difference that I am m

8条回答
  •  被撕碎了的回忆
    2020-12-02 12:30

    .h files, or header files, are used to list the publicly accessible instance variables and and methods in the class declaration. .cpp files, or implementation files, are used to actually implement those methods and use those instance variables.

    The reason they are separate is because .h files aren't compiled into binary code while .cpp files are. Take a library, for example. Say you are the author and you don't want it to be open source. So you distribute the compiled binary library and the header files to your customers. That allows them to easily see all the information about your library's classes they can use without being able to see how you implemented those methods. They are more for the people using your code rather than the compiler. As was said before: it's the convention.

提交回复
热议问题