← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 6 of 17

C++ की Header Files

एक header file एक separate file है जो बताती है कि code क्या offer करता है, एक table of contents की तरह। दूसरी files उस code को इस्तेमाल करने के लिए इसे include करती हैं।
Syntax
cpp
// 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?

cpp
// shape.h
class Shape {
public:
	double area();
};

// shape.cpp
#include "shape.h"
double Shape::area() { return 0.0; }

Header Guards

Header guards एक header file को same file में कई बार include होने से रोकते हैं। यह duplicate definition compiler errors से बचाता है।

उदाहरण: Header Guards

cpp
// shape.h
#ifndef SHAPE_H
#define SHAPE_H

class Shape {
public:
	double area();
};

#endif

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

cpp
// 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;
}

Headers में Classes Declare करना

अपना code organize करने के लिए, एक header file में class blueprints define करें और actual member functions एक corresponding source file में implement करें।

उदाहरण: Declaring Classes in Headers

cpp
// 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);
}

Headers में Inline Functions

अगर आप एक header के अंदर सीधे एक function define करते हैं, इसे inline declare करें। यह linker को अलग-अलग source files में function की कई definitions को बिना conflict errors cause किए allow करने को कहता है।

उदाहरण: Inline Functions in Headers

cpp
// mathutils.h
inline int square(int x) {
	return x * x; // "inline" avoids duplicate-definition linker errors
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Header guards भूल जाना, जो redefinition errors cause करता है।
  2. कई files से include होने वाले एक header में function definitions डालना।
  3. अपने खुद के headers के लिए quotes की बजाय angle brackets इस्तेमाल करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.