← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 7 of 17

C++ में Multi-file Programming

Multi-file programming एक बड़े program को कई files में split करती है, एक book के chapters की तरह। ये separately compile होती हैं और अंत में साथ join होती हैं।
Syntax
cpp
// 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

cpp
// Car.h
class Car {
public:
	void drive();
};

// Car.cpp
#include "Car.h"
#include <iostream>
void Car::drive() { std::cout << "Driving" << std::endl; }

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

cpp
// 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
}

extern से Variables Share करना

कई files में एक global variable share करने के लिए, अपनी header file में extern keyword इस्तेमाल करें। यह compiler को बताता है कि variable exist करता है, लेकिन आपको इसे सिर्फ एक source file में initialize करना ज़रूरी है।

उदाहरण: Sharing Variables with extern

cpp
// globals.h
extern int totalCars;

// globals.cpp
int totalCars = 0;

Isolation के लिए Static Functions

अगर आप एक function या global variable को restrict करना चाहें ताकि इसे सिर्फ उस file के अंदर इस्तेमाल किया जा सके जहाँ यह declare है, static keyword इस्तेमाल करें। यह दूसरी source files के साथ naming conflicts रोकता है।

उदाहरण: Static Functions for Isolation

cpp
// helpers.cpp
static int computeDiscount(int price) {
	return price / 10;
}

int applyDiscount(int price) {
	return price - computeDiscount(price);
}

Files के आर-पार Classes Organize करना

Object-oriented programming में, classes को files में neatly organize किया जाना चाहिए। class declaration अपनी header file में रखें और member function implementations source file में।

उदाहरण: Organizing Classes Across Files

cpp
// Account.h
class Account {
public:
	double getBalance();
private:
	double balance = 0;
};

// Account.cpp
#include "Account.h"
double Account::getBalance() { return balance; }
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. files के बीच एक global variable share करते वक्त extern भूल जाना।
  2. एक से ज़्यादा file में same function define करना।
  3. compile command में सारी .cpp files list करना भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.