Basic Program Structure of C++ (C++ प्रोग्राम का स्ट्रक्चर)

Every C++ program consists of different sections that perform specific tasks. These sections may be written in a single source file or organized into multiple files for better program organization and reusability. A general structure of a C++ program is shown below:

प्रत्येक C++ प्रोग्राम में अलग-अलग भाग होते हैं, जो विशेष कार्य करते हैं। इन भागों को एक ही Source File में लिखा जा सकता है या प्रोग्राम को बेहतर तरीके से व्यवस्थित करने तथा दोबारा उपयोग करने के लिए अलग-अलग Files में रखा जा सकता है। C++ प्रोग्राम का सामान्य प्रारूप निम्नलिखित है:

General Structure of a C++ Program

------------------------------------------------
1. Include Files (Header Files)
2. Global Declarations
- Global Variables
- Constants
- Function Prototypes
3. Class Declaration
- Data Members
- Member Function Declarations
4. Member Function Definitions
- Definitions of Class Member Functions
5. Main Function
- Variable Declarations
- Executable Statements
- Function Calls
6. Other Functions and Classes (Optional)
------------------------------------------------

1. Include Files (Header Files)

Header files are included at the beginning of a C++ program using the #include preprocessor directive. They provide declarations of library functions, objects, and other facilities required by the program.

C++ प्रोग्राम के प्रारंभ में #include Preprocessor Directive की सहायता से Header Files को शामिल किया जाता है। ये प्रोग्राम में आवश्यक Library Functions, Objects तथा अन्य सुविधाओं के Declarations प्रदान करती हैं।

Example:

#include <iostream>

2. Global Declarations (ग्लोबल डिक्लेरेशन)

Global variables, constants, and function prototypes can be declared outside all functions and classes. These declarations are generally accessible throughout the relevant source file, subject to their scope and linkage.

Global Variables, Constants तथा Function Prototypes को सभी Functions और Classes के बाहर Declare किया जा सकता है। ये Declarations सामान्यतः संबंधित Source File में उपलब्ध होते हैं, जो उनके Scope तथा Linkage पर निर्भर करता है।

Example:

int count = 0;
float rate = 5.5;
void display();

3. Class Declaration (क्लास डिक्लेरेशन)

A class declaration defines the structure of a class. It contains data members and declarations of member functions. A class provides a blueprint for creating objects.

Class Declaration किसी Class की संरचना को परिभाषित करता है। इसमें Data Members तथा Member Functions के Declarations होते हैं। Class Objects बनाने के लिए एक Blueprint प्रदान करती है।

Example:

class Student
{
private:
int roll;
public:
void getData();
void displayData();
};

4. Member Function Definitions (मेंबर फंक्शन डेफिनिशन)

Member functions can be defined inside or outside the class. When they are defined outside the class, the Scope Resolution Operator (::) is used to associate the functions with their class.

Member Functions को Class के अंदर या बाहर Define किया जा सकता है। जब इन्हें Class के बाहर Define किया जाता है, तब Scope Resolution Operator (::) का उपयोग करके Functions को उनकी Class से जोड़ा जाता है।

Example:

void Student::getData()
{
std::cout << "Enter Roll Number: ";
std::cin >> roll;
}

5. Main Function (मेन फंक्शन)

The main() function is the entry point of a C++ program. Program execution normally begins from this function. It contains variable declarations, executable statements, and function calls.

main() Function C++ प्रोग्राम का Entry Point होता है। सामान्यतः प्रोग्राम का Execution इसी Function से प्रारंभ होता है। इसमें Variable Declarations, Executable Statements तथा Function Calls लिखे जाते हैं।

Example:

int main()
{
int a, b, sum;
a = 10;
b = 20;
sum = a + b;
std::cout << "Sum = " << sum;
return 0;
}

6. Other Functions and Classes (अन्य फंक्शन्स एवं क्लासेस)

A C++ program may contain additional user-defined functions and classes to perform different tasks. These functions and classes help divide a large program into smaller and manageable parts.

C++ प्रोग्राम में अलग-अलग कार्यों को पूरा करने के लिए अन्य User-Defined Functions तथा Classes भी हो सकती हैं। इनकी सहायता से बड़े प्रोग्राम को छोटे एवं व्यवस्थित भागों में विभाजित किया जा सकता है।

Multiple File Organization in C++ (C++ में मल्टीपल फाइल ऑर्गेनाइजेशन)

In C++, a large program can be organized into multiple files. This approach makes the program easier to maintain, modify, and reuse. A common arrangement uses three separate files:

C++ में बड़े प्रोग्राम को Multiple Files में व्यवस्थित किया जा सकता है। इस तरीके से प्रोग्राम को Maintain करना, Modify करना तथा दोबारा उपयोग करना आसान हो जाता है। सामान्यतः इसमें तीन अलग-अलग Files का उपयोग किया जाता है:

  1. Header File (.h): Contains class declarations, data members, and member function declarations.

    इसमें Class Declarations, Data Members तथा Member Function Declarations रखे जाते हैं।

  2. Implementation File (.cpp): Contains definitions of class member functions.

    इसमें Class के Member Functions की Definitions रखी जाती हैं।

  3. Main Program File (.cpp): Contains the main() function and the code that uses the class.

    इसमें main() Function तथा Class का उपयोग करने वाला Code रखा जाता है।

Note: This organization is similar to the client-server approach in the sense that the main program acts as a client using the public interface provided by a class. However, it is more accurately described as modular programming or separate compilation.

ध्यान दें: यह व्यवस्था Client-Server Approach के समान दिखाई देती है, क्योंकि Main Program एक Client की तरह Class के Public Interface का उपयोग करता है। हालांकि, तकनीकी रूप से इसे Modular Programming या Separate Compilation कहना अधिक सही है।

Example 1: C++ Program to Calculate Simple Interest

Formula:

Simple Interest = (Principal × Rate × Time) / 100

Program:

#include <iostream>
using namespace std;
int main()
{
float p, r, t, si;
cout << "Enter Principal, Rate and Time:" << endl;
cin >> p >> r >> t;
si = (p * r * t) / 100;
cout << "Simple Interest = " << si << endl;
return 0;
}

Output:

Enter Principal, Rate and Time:
10000 5 2
Simple Interest = 1000

In this program, three variables p, r, and t are used to accept Principal, Rate, and Time from the user. The Simple Interest is calculated using the given formula and displayed on the screen.

इस प्रोग्राम में p, r तथा t तीन Variables का उपयोग Principal, Rate और Time को Input करने के लिए किया गया है। दिए गए Formula की सहायता से Simple Interest की गणना करके स्क्रीन पर Display किया जाता है।

Example 2: C++ Program to Find Sum and Average of Two Numbers

Program:

#include <iostream>
using namespace std;
int main()
{
float a, b, sum, avg;
cout << "Enter Two Numbers:" << endl;
cin >> a >> b;
sum = a + b;
cout << "Sum = " << sum << endl;
avg = sum / 2;
cout << "Average = " << avg << endl;
return 0;
}

Output:

Enter Two Numbers:
10 20
Sum = 30
Average = 15

In this program, two numbers are accepted from the user. Their sum is calculated and stored in the variable sum. The average is calculated by dividing the sum by 2.

इस प्रोग्राम में User से दो संख्याएँ Input की जाती हैं। उनका योग निकालकर sum Variable में Store किया जाता है। इसके बाद Sum को 2 से Divide करके Average की गणना की जाती है।

The basic structure of a C++ program helps programmers organize different parts of a program systematically. Header files, global declarations, class declarations, member function definitions, and the main function are important components of program organization. For large applications, multiple-file organization makes programs easier to maintain and reuse.

C++ प्रोग्राम का Basic Structure Programmer को प्रोग्राम के विभिन्न भागों को व्यवस्थित तरीके से लिखने में सहायता करता है। Header Files, Global Declarations, Class Declarations, Member Function Definitions तथा Main Function प्रोग्राम के महत्वपूर्ण भाग हैं। बड़े Applications में Multiple File Organization प्रोग्राम को Maintain करने तथा दोबारा उपयोग करने में सहायता करता है।

Comments

Popular posts from this blog

Important Questions of C++ including OOP's

Programming Methodologies and OOP's using C++ Language

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)