Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 13

Header Files in C++

In this lesson: Standard C++ headers, creating your own .h files and include guards / #pragma once — with examples. In Hindi & English.

What is a header file?

A header file holds *declarations* that more than one part of a program needs: function prototypes, class definitions, constants and templates. When you write #include <iostream>, the preprocessor copies the whole text of that header into your file before the compiler sees it. That is how the compiler knows what std::cout is.

Header = the "menu" (what exists and how to call it). Source file (.cpp) = the "kitchen" (how it actually works).

Two ways to include

C++
#include <vector>        // angle brackets: search the standard/system folders
#include "student.h"     // quotes: search your project folder first

Use < > for standard library and installed library headers, and " " for headers you wrote.

Most-used standard headers

HeaderWhat it gives you
<iostream>cin, cout, cerr, endl
<iomanip>setw, setprecision, fixed for formatted output
<string>std::string and its functions
<vector>std::vector dynamic array
<cmath>sqrt, pow, abs, sin, ceil, floor
<algorithm>sort, find, max_element, reverse
<fstream>ifstream, ofstream for files
<map>, <set>std::map, std::set
<memory>unique_ptr, shared_ptr, make_unique
<cstdlib>rand, exit, abs (from C)
<ctime>time — often used with srand

C library headers have a C++ version with a c prefix and no .h: use <cmath> instead of <math.h>, <cstring> instead of <string.h>.

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 is provided by GCC/Clang's <cmath>; on MSVC define _USE_MATH_DEFINES first or use your own constexpr double PI.)

Writing your own header

Split a program into three files: the header declares, one .cpp defines, and main.cpp uses.

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)

Only .cpp files go on the compile command; headers are pulled in by #include.

Include guards and #pragma once

If student.h gets included twice (directly and through another header), class Student would be defined twice — an error. The #ifndef / #define / #endif lines above prevent that: the second time, STUDENT_H is already defined, so the content is skipped.

Most compilers also accept a one-line alternative at the top of the header:

C++
#pragma once

It is not part of the official standard but is supported by GCC, Clang and MSVC. Either approach is fine; just use one in every header you write.

What goes where

Put in the header (.h)Put in the source (.cpp)
Class definitionsMember function bodies
Function prototypesFunction bodies
constexpr/const constantsGlobal variable definitions (if any)
Templates and inline functions (whole code)Helper functions used only in that file
Include guards#include of headers needed only for the implementation

iostream.h, conio.h and bits/stdc++.h

  • <iostream.h> and <conio.h> (clrscr(), getch()) come from Turbo C++. They do not exist in standard C++ — use <iostream> and drop clrscr()/getch().
  • <bits/stdc++.h> includes the entire library. It works only on GCC and slows compilation; fine in competitive programming, but avoid it in school projects and real code.

Common mistakes

  • Writing #include <iostream.h> or #include "iostream" for standard headers.
  • Forgetting include guards and getting "redefinition of class" errors.
  • Defining a normal function in a header included by two .cpp files — "multiple definition" at link time.
  • Putting using namespace std; in a header, which leaks into every file that includes it.
  • Compiling only main.cpp and getting "undefined reference" because student.cpp was not compiled.
🏋️ Practice

Create mathutils.h with prototypes for int factorial(int) and bool isPrime(int), implement them in mathutils.cpp, and use both from main.cpp. Add include guards, then include the header twice in main.cpp to confirm the guard works.

Summary

  • Headers contain declarations; #include copies them in before compilation.
  • < > for standard/library headers, " " for your own.
  • Use <cmath>-style C++ headers, never <iostream.h>.
  • Protect every header with include guards or #pragma once.
  • Declarations in .h, definitions in .cpp; templates and inline functions stay in the header.

Related: preprocessor directives, namespaces, structure of a C++ program.

Frequently Asked Questions

What is a header file in C++?
A header file (usually .h or .hpp, or no extension for standard headers like <iostream>) contains declarations — function prototypes, class definitions, constants and templates — that several source files need. #include copies its text into your file before compilation so the compiler knows those names.
What is the difference between and ?
<iostream> is the standard C++ header and puts its names in namespace std. <iostream.h> is a pre-standard header from old compilers like Turbo C++; modern compilers such as GCC, Clang and MSVC do not provide it, so always use <iostream>.
What are include guards and why are they needed?
If a header is included twice in the same translation unit, its class definitions appear twice and the compiler reports a redefinition error. Include guards (#ifndef NAME_H / #define NAME_H / ... / #endif) or #pragma once make sure the header's content is used only once.
Should I put function definitions in a header file?
Put declarations in the header and definitions in a matching .cpp file. Exceptions are templates, inline functions and constexpr functions, which normally live entirely in the header. Putting ordinary function definitions in a header that is included in several .cpp files causes "multiple definition" linker errors.
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicC++ Coding Best Practices Your progress: 13 / 81 (16%) · Lesson 14 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free