C++ में Multi-file Programming
In this page:
// filename.h (declarations)
return_type function_name(parameters);
// filename.cpp (definitions)
#include "filename.h"
return_type function_name(parameters) { }
// main.cpp
#include "filename.h"
Multi-file Projects का Introduction
बड़े C++ projects code को manage करना आसान बनाने के लिए कई files में split होते हैं। Typically, हर class या module को declarations के लिए अपनी header file (.h) और implementations के लिए अपनी source file (.cpp) मिलती है।
उदाहरण: Introduction to Multi-file Projects
// Car.h
class Car {
public:
void drive();
};
// Car.cpp
#include "Car.h"
#include <iostream>
void Car::drive() { std::cout << "Driving" << std::endl; }
Login to try C/C++/Java/PHP code in the editor
Compilation Process
जब आप एक multi-file project compile करते हैं, compiler हर source file (.cpp) को individually एक object file (.o या .obj) में compile करता है। Linker फिर इन सभी object files को एक single runnable program में combine करता है।
उदाहरण: The Compilation Process
// main.cpp
#include "Car.h"
int main() {
Car car;
car.drive();
return 0;
// each .cpp compiles to its own .o file, then the linker combines them
}
Login to try C/C++/Java/PHP code in the editor
extern से Variables Share करना
कई files में एक global variable share करने के लिए, अपनी header file में extern keyword इस्तेमाल करें। यह compiler को बताता है कि variable exist करता है, लेकिन आपको इसे सिर्फ एक source file में initialize करना ज़रूरी है।
उदाहरण: Sharing Variables with extern
// globals.h
extern int totalCars;
// globals.cpp
int totalCars = 0;
Login to try C/C++/Java/PHP code in the editor
Isolation के लिए Static Functions
अगर आप एक function या global variable को restrict करना चाहें ताकि इसे सिर्फ उस file के अंदर इस्तेमाल किया जा सके जहाँ यह declare है, static keyword इस्तेमाल करें। यह दूसरी source files के साथ naming conflicts रोकता है।
उदाहरण: Static Functions for Isolation
// helpers.cpp
static int computeDiscount(int price) {
return price / 10;
}
int applyDiscount(int price) {
return price - computeDiscount(price);
}
Login to try C/C++/Java/PHP code in the editor
Files के आर-पार Classes Organize करना
Object-oriented programming में, classes को files में neatly organize किया जाना चाहिए। class declaration अपनी header file में रखें और member function implementations source file में।
उदाहरण: Organizing Classes Across Files
// Account.h
class Account {
public:
double getBalance();
private:
double balance = 0;
};
// Account.cpp
#include "Account.h"
double Account::getBalance() { return balance; }
Login to try C/C++/Java/PHP code in the editor
- files के बीच एक global variable share करते वक्त
externभूल जाना। - एक से ज़्यादा file में same function define करना।
- compile command में सारी
.cppfiles list करना भूल जाना।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced