0% found this document useful (0 votes)
27 views3 pages

Header File in CPP Notes

A header file in C++ is a `.h` or `.hpp` file that contains function and class declarations, promoting code reusability and organization. It typically includes include guards to prevent multiple inclusions and is essential for maintaining clean and modular code in larger projects. The document provides examples of creating and using header files for functions and classes, along with best practices for their implementation.

Uploaded by

MD Waseem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Header File in CPP Notes

A header file in C++ is a `.h` or `.hpp` file that contains function and class declarations, promoting code reusability and organization. It typically includes include guards to prevent multiple inclusions and is essential for maintaining clean and modular code in larger projects. The document provides examples of creating and using header files for functions and classes, along with best practices for their implementation.

Uploaded by

MD Waseem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating a Header File in C++

What is a Header File in C++?


A header file in C++ is a file with the extension `.h` or `.hpp` that
contains:
- Function declarations
- Class declarations
- Constant definitions
- Macro definitions

It helps to separate declarations from definitions and promotes code


reusability and organization.

Why Use Header Files?


✅ Easy to reuse functions and classes in multiple `.cpp` files
✅ Keeps the code clean and modular
✅ Helps in large projects by splitting code into smaller parts

Structure of a Header File


A header file usually contains:
1. Function or class declarations
2. Include guards (to prevent multiple inclusions)

Example 1: Simple Function in a Header File

math_operations.h – Header File


// Header guards to prevent multiple inclusions
#ifndef MATH_OPERATIONS_H
#define MATH_OPERATIONS_H

// Function declarations
int add(int a, int b);
int multiply(int a, int b);

#endif

math_operations.cpp – Function Definitions


#include "math_operations.h"

int add(int a, int b) {


return a + b;
}

int multiply(int a, int b) {


return a * b;
}
main.cpp – Main Program Using Header
#include <iostream>
#include "math_operations.h" // Including the header file

int main() {
int x = 5, y = 3;

std::cout << "Sum: " << add(x, y) << std::endl;


std::cout << "Product: " << multiply(x, y) << std::endl;

return 0;
}

Example 2: Using Class in Header File

Calculator.h – Header File for a Class


#ifndef CALCULATOR_H
#define CALCULATOR_H

class Calculator {
public:
int add(int a, int b);
int subtract(int a, int b);
};

#endif

Calculator.cpp – Class Method Definitions


#include "Calculator.h"

int Calculator::add(int a, int b) {


return a + b;
}

int Calculator::subtract(int a, int b) {


return a - b;
}

main.cpp – Using the Class


#include <iostream>
#include "Calculator.h"

int main() {
Calculator calc;

std::cout << "Add: " << calc.add(10, 5) << std::endl;


std::cout << "Subtract: " << calc.subtract(10, 5) << std::endl;
return 0;
}

Tips for Header Files


📌 Always use include guards (#ifndef, #define, #endif) to avoid multiple inclusion errors.
📌 Don’t write function definitions in `.h` files (only declarations), unless using inline
functions or templates.
📌 Use `.cpp` files to define the actual logic.

Conclusion
Creating and using header files in C++ helps organize and manage code efficiently,
especially in large projects. It separates declaration from definition and allows for code
reuse across multiple files.

You might also like