Lecture 25 - Header Files | C/C++ Video Tutorials and Lectures for Beginners

preview_player
Показать описание

Welcome to this session of my video series!
In this tutorial I shall discuss about the header files in C and C++.
I shall discuss why do we need header files.
We shall also know what should be contained in header files.
And, also, what should not be contained in header files.
Finally, we shall end with an explanation of header guards.
So first let us understand why are header files required.
This is a simple program.
This is a declaration for a function FX.
A declaration is an introduction of a function.
It tells the compiler that a function with this name and these parameters exists.
It is the job of the linker to locate its definition.
The definition could be in the same CPP file.
It could be just after the main function.
The definition can also exist in some other CPP file of this project.
The definition could be in an external compiled LIB file also.
This is usually so.
For example the function PRINTF is in an external LIB file.
In our program the definition is placed after the main function.
The definition contains the code body of the function.
This program compiles correctly, and runs correctly.
The compilation will fail if this line is removed.
The compilation fails if this declaration is removed.
The compiler error will come at this point.
The compiler doesn't know anything about FX.
The function wasn't introduced to it.
In C++ we should remember some rules which I explain one by one.
A function declaration should be visible before it is used.
A compiler error occurs if declaration is not made prior to use.
More than one declarations of the same function are allowed.
Let us see this code again.
More than one declarations are allowed.
So, a function can be declared more than once.
The code will compile without errors.
But there can be only one function definition.
More than one definition with the same signature will cause linker errors.
Only one definition is allowed.
Let me next take the situation with class definitions.
This is how a class definition looks like.
It can contain function definitions also.
The definition of a class, struct and union should appear before objects are created.
It is just like a function declaration which has to appear before use.
On the same pattern, the definition of a class has to appear prior to use.
There can be only one definition of a class.
More than one definition will cause error.
So multiple function declarations are allowed before use.
But multiple class definitions are not allowed before using it for creating objects.
Function declarations can be written inside headers.
We can include the header files wherever we need to use a particular function.
There is no need to type again and again.
There is no chance of typing errors also.
Let us see our very first program again.
This header contains the declaration of the PRINTF function.
You can now appreciate the use of a header file because we didn't have to type anything.
Similarly, the headers can contain class definitions.
Whenever we need to create an object of a class we can include that header file.
Sometimes a header can get included twice.
It is not obvious at first sight but it occurs very commonly.
Let us see how.
Suppose this is a header file.
Suppose that this is another header file.
Let us suppose that this header file includes the earlier one.
It is possible and happens very frequently.
What happens if both the above headers are included in this CPP file?
You can very well see that the ABC header gets included twice.
Once it is included directly.
And, secondly it gets included through DEF.
There will be no problem if the file ABC contains a declaration of FX.
There will be no problem because multiple declarations are allowed.
So no problems will occur because two declarations of FX can exist in one CPP file.
The situation becomes different if this class definition is present in this header.
It would lead to two definitions of the same class.
An error will occur.
The program cannot be run.
So how to solve this problem?
The problem can be solved by using header guards.
Now I will explain how to prevent multiple class definitions.
This is a matched IF and ELSE.
These are preprocessor conditions.
The code inside this IF and ELSE pair is compiled only if the symbol HOVEN is undefined.
HOVEN is UN-defined when this header file is included the first time.
So this class definition gets included.
After that the same symbol HOVEN is defined.
This is a sort of a trick.
It acts as a sort of a guard to prevent the class definition from appearing twice.
This symbol HOVEN is already defined when this header is included the second time.
So the class definition is prevented from appearing again.
This is a typical arrangement of a header file.
The name of the header file in upper case is used as the guard symbol.
Рекомендации по теме
Комментарии
Автор

very nice tutorial. By the way you talk like osho!!

Aman-nwjp
welcome to shbcf.ru