Header Files in C++
Header file क्या है?
Header file में वे *declarations* होती हैं जिनकी program के एक से ज़्यादा हिस्सों को ज़रूरत है: function prototypes, class definitions, constants और templates। जब आप #include <iostream> लिखते हैं, तो preprocessor उस header का पूरा text compiler के देखने से पहले आपकी file में copy कर देता है। इसी से compiler को पता चलता है कि std::cout क्या है।
.cpp) = "रसोई" (असल में काम कैसे होता है)।Include करने के दो तरीके
#include <vector> // angle brackets: standard/system folders में खोजो #include "student.h" // quotes: पहले अपने project folder में खोजो
Standard library और install की गई libraries के headers के लिए < >, और अपने लिखे headers के लिए " " इस्तेमाल करें।
सबसे ज़्यादा इस्तेमाल होने वाले standard headers
| Header | इससे क्या मिलता है |
|---|---|
<iostream> | cin, cout, cerr, endl |
<iomanip> | Formatted output के लिए setw, setprecision, fixed |
<string> | std::string और उसके functions |
<vector> | std::vector dynamic array |
<cmath> | sqrt, pow, abs, sin, ceil, floor |
<algorithm> | sort, find, max_element, reverse |
<fstream> | Files के लिए ifstream, ofstream |
<map>, <set> | std::map, std::set |
<memory> | unique_ptr, shared_ptr, make_unique |
<cstdlib> | rand, exit, abs (C से) |
<ctime> | time — अक्सर srand के साथ |
C library headers का C++ version c prefix और बिना .h के होता है: <math.h> की जगह <cmath>, <string.h> की जगह <cstring>।
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
double r = 2.5;
std::cout << std::fixed << std::setprecision(2)
<< "Area = " << M_PI * std::pow(r, 2) << "\n"
<< "sqrt(2) = " << std::sqrt(2.0) << "\n";
return 0;
}(M_PI GCC/Clang के <cmath> में मिलता है; MSVC पर पहले _USE_MATH_DEFINES define करें या अपना constexpr double PI बनाएँ।)
अपनी header file बनाना
Program को तीन files में बाँटें: header declare करता है, एक .cpp define करती है, और main.cpp इस्तेमाल करती है।
// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class Student {
public:
Student(std::string name, int marks);
void show() const;
bool passed() const;
private:
std::string name_;
int marks_;
};
#endif // STUDENT_H// student.cpp
#include "student.h"
#include <iostream>
Student::Student(std::string name, int marks)
: name_(name), marks_(marks) {}
void Student::show() const {
std::cout << name_ << ": " << marks_
<< (passed() ? " (Pass)" : " (Fail)") << "\n";
}
bool Student::passed() const { return marks_ >= 33; }// main.cpp
#include "student.h"
int main() {
Student a("Aman", 78), b("Ravi", 29);
a.show();
b.show();
return 0;
}g++ main.cpp student.cpp -o result ./result
Compile command में सिर्फ .cpp files आती हैं; headers #include से अपने-आप जुड़ते हैं।
Include guards और #pragma once
अगर student.h दो बार include हो जाए (सीधे भी और किसी दूसरे header के ज़रिए भी), तो class Student दो बार define होगी — error। ऊपर की #ifndef / #define / #endif lines इसे रोकती हैं: दूसरी बार STUDENT_H पहले से defined होता है, इसलिए content छोड़ दिया जाता है।
ज़्यादातर compilers header के ऊपर एक line वाला विकल्प भी मानते हैं:
#pragma once
यह official standard का हिस्सा नहीं है, पर GCC, Clang और MSVC इसे support करते हैं। कोई भी तरीका चलेगा; बस हर header में एक ज़रूर लगाएँ।
क्या कहाँ रखें
| Header (.h) में | Source (.cpp) में |
|---|---|
| Class definitions | Member functions के body |
| Function prototypes | Functions के body |
constexpr/const constants | Global variable definitions (अगर हों) |
Templates और inline functions (पूरा code) | सिर्फ उसी file में इस्तेमाल होने वाले helper functions |
| Include guards | सिर्फ implementation के लिए ज़रूरी headers का #include |
iostream.h, conio.h और bits/stdc++.h
<iostream.h>और<conio.h>(clrscr(),getch()) Turbo C++ के हैं। Standard C++ में ये नहीं हैं —<iostream>लें औरclrscr()/getch()हटा दें।<bits/stdc++.h>पूरी library include कर देता है। यह सिर्फ GCC पर चलता है और compilation धीमा करता है; competitive programming में ठीक, पर school projects और असली code में इससे बचें।
आम गलतियाँ
- Standard headers के लिए
#include <iostream.h>या#include "iostream"लिखना। - Include guards भूलना और "redefinition of class" errors पाना।
- ऐसे header में साधारण function define करना जो दो
.cppfiles में include हो — link time पर "multiple definition"। - Header में
using namespace std;रखना, जो उसे include करने वाली हर file में पहुँच जाता है। - सिर्फ
main.cppcompile करना और "undefined reference" पाना क्योंकिstudent.cppcompile नहीं हुई।
mathutils.h बनाएँ जिसमें int factorial(int) और bool isPrime(int) के prototypes हों, उन्हें mathutils.cpp में लिखें, और main.cpp से दोनों इस्तेमाल करें। Include guards लगाएँ, फिर main.cpp में header दो बार include करके जाँचें कि guard काम कर रहा है।
सारांश
- Headers में declarations होती हैं;
#includeउन्हें compilation से पहले copy करता है। - Standard/library headers के लिए
< >, अपने headers के लिए" "। <cmath>जैसे C++ headers लें,<iostream.h>कभी नहीं।- हर header को include guards या
#pragma onceसे सुरक्षित करें। .hमें declarations,.cppमें definitions; templates और inline functions header में ही रहते हैं।
संबंधित: preprocessor directives, namespaces, C++ program की संरचना।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में header file क्या है?
.h या .hpp, या standard headers जैसे <iostream> में बिना extension) में declarations होती हैं — function prototypes, class definitions, constants और templates — जिनकी कई source files को ज़रूरत होती है। #include compilation से पहले उसका text आपकी file में copy कर देता है ताकि compiler उन नामों को पहचान सके। और में क्या अंतर है?
<iostream> standard C++ header है और अपने नाम std namespace में रखता है। <iostream.h> Turbo C++ जैसे पुराने compilers का pre-standard header है; GCC, Clang और MSVC जैसे modern compilers इसे नहीं देते, इसलिए हमेशा <iostream> लिखें।Include guards क्या हैं और क्यों ज़रूरी हैं?
#ifndef NAME_H / #define NAME_H / ... / #endif) या #pragma once पक्का करते हैं कि header का content एक ही बार इस्तेमाल हो।क्या function definitions header file में रखनी चाहिए?
.cpp file में definitions रखें। Templates, inline functions और constexpr functions अपवाद हैं, जो आमतौर पर पूरे header में ही रहते हैं। साधारण function definitions ऐसे header में रखने पर, जो कई .cpp files में include हो, "multiple definition" linker error आता है।