.hfiles, allow related function, method, and class declarations to be collected in one place.”. This is useful as you can group together specific functionality and then export and use it at another place/file to be used by another program. There are 2 file extensions that we deal with “.h” and “.cpp”. The header file will have a “.h” extension whereas, the corresponding functions, methods, and classes will be contained within files that will have the ‘.cpp’ extension. You will include the ‘.h’ or header file in the ‘.cpp’ file with:
/*include header file which is in the same directory as main.cpp notice that if we in include a header file written by a programmer we use "" and not < > */ #include "header_file.h";
2 Types of header files:
- System header files: Comes with the compiler
- User header files: Written by the programmer
Header file structure:The header file contains some pre-processor directives:
- #ifndef: stands for “if not defined” for example : #ifndef HEADER_FILE_H
- #define: So if the header file is not defined, then define it
- #endif : End the if statement
// This file will be saved as "header_practice.h" #ifndef HEADER_PRACTICE_H #define HEADER_PRACTICE_H //we need this as the function declaration needs it #include <vector> using std::vector; int IncrementAndComputeVectorSum(vector<int> v); void AddOneToEach(vector<int> &v); #endif
Join 7000+ other code newbies
Last year 7000 newbies learned how to code with me! Totally project oriented.
Subscribe and you'll get access to my classes, coupon codes for tech conferences and totally valuable article on jump-starting your career as a developer.