Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Foundation  ·  Lesson 13

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 क्या है।

Header = "menu" (क्या-क्या मौजूद है और कैसे call करना है)। Source file (.cpp) = "रसोई" (असल में काम कैसे होता है)।

Include करने के दो तरीके

C++
#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>।

C++
#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;
}
Area = 19.63 sqrt(2) = 1.41

(M_PI GCC/Clang के <cmath> में मिलता है; MSVC पर पहले _USE_MATH_DEFINES define करें या अपना constexpr double PI बनाएँ।)

अपनी header file बनाना

Program को तीन files में बाँटें: header declare करता है, एक .cpp define करती है, और main.cpp इस्तेमाल करती है।

C++
// 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
C++
// 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; }
C++
// main.cpp
#include "student.h"

int main() {
    Student a("Aman", 78), b("Ravi", 29);
    a.show();
    b.show();
    return 0;
}
Terminal
g++ main.cpp student.cpp -o result
./result
Aman: 78 (Pass) Ravi: 29 (Fail)

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 वाला विकल्प भी मानते हैं:

C++
#pragma once

यह official standard का हिस्सा नहीं है, पर GCC, Clang और MSVC इसे support करते हैं। कोई भी तरीका चलेगा; बस हर header में एक ज़रूर लगाएँ।

क्या कहाँ रखें

Header (.h) मेंSource (.cpp) में
Class definitionsMember functions के body
Function prototypesFunctions के body
constexpr/const constantsGlobal 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 करना जो दो .cpp files में include हो — link time पर "multiple definition"।
  • Header में using namespace std; रखना, जो उसे include करने वाली हर file में पहुँच जाता है।
  • सिर्फ main.cpp compile करना और "undefined reference" पाना क्योंकि student.cpp compile नहीं हुई।
🏋️ अभ्यास

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 क्या है?
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 क्या हैं और क्यों ज़रूरी हैं?
अगर एक ही header किसी file में दो बार include हो जाए, तो उसकी class definitions दो बार आती हैं और compiler redefinition error देता है। Include guards (#ifndef NAME_H / #define NAME_H / ... / #endif) या #pragma once पक्का करते हैं कि header का content एक ही बार इस्तेमाल हो।
क्या function definitions header file में रखनी चाहिए?
Header में declarations और उससे मिलती .cpp file में definitions रखें। Templates, inline functions और constexpr functions अपवाद हैं, जो आमतौर पर पूरे header में ही रहते हैं। साधारण function definitions ऐसे header में रखने पर, जो कई .cpp files में include हो, "multiple definition" linker error आता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicC++ Coding Best Practices आपकी प्रगति: 13 / 81 (16%) · Lesson 14 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें