C++ की Header Files
In this page:
// filename.h
#ifndef FILENAME_H
#define FILENAME_H
// declarations
#endif
// filename.cpp
#include "filename.h"
Header Files क्यों Use करें?
Header files declarations को implementations से separate करने के लिए इस्तेमाल होती हैं। इनमें function declarations, class definitions, और macro definitions होती हैं। यह आपको कई source files में declarations share करने देता है।
उदाहरण: Why Use Header Files?
// shape.h
class Shape {
public:
double area();
};
// shape.cpp
#include "shape.h"
double Shape::area() { return 0.0; }
Login to try C/C++/Java/PHP code in the editor
Header Guards
Header guards एक header file को same file में कई बार include होने से रोकते हैं। यह duplicate definition compiler errors से बचाता है।
उदाहरण: Header Guards
// shape.h
#ifndef SHAPE_H
#define SHAPE_H
class Shape {
public:
double area();
};
#endif
Login to try C/C++/Java/PHP code in the editor
System Headers बनाम User Headers
System headers angle brackets इस्तेमाल करके include होती हैं, जैसे <vector>। यह compiler को पहले system directories search करने को कहता है।
Local user headers double quotes इस्तेमाल करके include होती हैं, जैसे "MyHeader.h", जो compiler को पहले आपका project folder search करने को कहता है।
उदाहरण: System Headers vs User Headers
// Include std::cout and std::cin
#include <iostream>
#include "myheader.h"
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "Angle brackets vs quotes" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Headers में Classes Declare करना
अपना code organize करने के लिए, एक header file में class blueprints define करें और actual member functions एक corresponding source file में implement करें।
उदाहरण: Declaring Classes in Headers
// point.h
class Point {
public:
int x, y;
double distanceFromOrigin();
};
// point.cpp
#include "point.h"
#include <cmath>
double Point::distanceFromOrigin() {
return std::sqrt(x * x + y * y);
}
Login to try C/C++/Java/PHP code in the editor
Headers में Inline Functions
अगर आप एक header के अंदर सीधे एक function define करते हैं, इसे inline declare करें। यह linker को अलग-अलग source files में function की कई definitions को बिना conflict errors cause किए allow करने को कहता है।
उदाहरण: Inline Functions in Headers
// mathutils.h
inline int square(int x) {
return x * x; // "inline" avoids duplicate-definition linker errors
}
Login to try C/C++/Java/PHP code in the editor
- Header guards भूल जाना, जो redefinition errors cause करता है।
- कई files से include होने वाले एक header में function definitions डालना।
- अपने खुद के headers के लिए quotes की बजाय angle brackets इस्तेमाल करना।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced