Block, Block Statement and Compound Statement in C++ C++ में ब्लॉक, ब्लॉक स्टेटमेंट एवं कंपाउंड स्टेटमेंट Block (ब्लॉक)

A block is a compound statement that consists of zero or more statements enclosed within curly braces { }. A block is treated as a single statement wherever a statement is expected in a program.

ब्लॉक एक कंपाउंड स्टेटमेंट होता है, जिसमें शून्य या उससे अधिक Statements का समूह Curly Braces { } के अंदर लिखा जाता है। प्रोग्राम में जहाँ भी एक Statement की आवश्यकता होती है, वहाँ Block का उपयोग एक Single Statement के रूप में किया जा सकता है।

A block begins with an opening curly brace { and ends with a closing curly brace }.

किसी Block का प्रारंभ Opening Curly Brace { से तथा अंत Closing Curly Brace } से होता है।

Syntax:

{
    statement1;
    statement2;
    statement3;
}

Block Statement and Compound Statement

In C++, the terms block, block statement, and compound statement are commonly used to describe a group of statements enclosed within curly braces { }.

C++ में Block, Block Statement और Compound Statement शब्दों का उपयोग सामान्यतः Curly Braces { } के अंदर लिखे गए Statements के समूह को दर्शाने के लिए किया जाता है।

Blocks are commonly used with functions, classes, conditional statements, loops, and other control structures.

Blocks का उपयोग Functions, Classes, Conditional Statements, Loops तथा अन्य Control Structures के साथ किया जाता है।

Example: Program to Demonstrate Block and Object Destruction in C++

#include <iostream>
using namespace std;

int count = 0;

class MyList
{
public:
    int i;

    MyList()
    {
        i = 0;
        count++;

        cout << "Object Created" << endl;
    }

    ~MyList()
    {
        cout << count << " Object Destroyed" << endl;
        count--;
    }
};

int main()
{
    MyList l1;

    cout << "\nEntering Block1..." << endl;

    // Block1
    {
        MyList l2, l3, l4;
    }

    cout << "\nThree Objects of Block1 Destroyed" << endl;

    cout << "Back To main function's block" << endl;

    cout << "\nObject of Main Block will be Destroyed" << endl;

    return 0;
}

Output:

Object Created

Entering Block1...
Object Created
Object Created
Object Created
4 Object Destroyed
3 Object Destroyed
2 Object Destroyed

Three Objects of Block1 Destroyed
Back To main function's block

Object of Main Block will be Destroyed
1 Object Destroyed

Explanation (व्याख्या)

In this program, a global variable count is declared and initialized to zero. A class named MyList is created with a constructor and a destructor.

इस प्रोग्राम में count नाम का एक Global Variable Declare किया गया है, जिसका प्रारंभिक मान शून्य है। इसके बाद MyList नाम की एक Class बनाई गई है, जिसमें एक Constructor और एक Destructor है।

1. Constructor

The constructor is called automatically whenever an object of the class is created. In this program, it increases the value of count by one and displays the message "Object Created".

जब भी इस Class का कोई Object Create किया जाता है, तब Constructor अपने आप Call होता है। इस प्रोग्राम में Constructor count का मान एक से बढ़ाता है और "Object Created" Message Display करता है।

2. Destructor

The destructor is called automatically when an object is destroyed. It displays the current value of count and then decreases it by one.

जब किसी Object का जीवनकाल समाप्त होता है, तब उसका Destructor अपने आप Call होता है। यह पहले count का वर्तमान मान Display करता है और फिर उसे एक से कम कर देता है।

3. Main Function Block

The object l1 is created inside the block of the main() function. Therefore, it remains alive until the execution of the main() function is completed.

Object l1 को main() Function के Block के अंदर Create किया गया है। इसलिए यह main() Function का Execution पूरा होने तक जीवित रहता है।

4. Block1

Inside Block1, three objects l2, l3, and l4 are created.

Block1 के अंदर तीन Objects l2, l3 और l4 Create किए गए हैं।

When the closing curly brace } of Block1 is reached, all three objects are automatically destroyed because their scope has ended.

जब Block1 की Closing Curly Brace } तक Execution पहुँचता है, तब तीनों Objects अपने आप Destroy हो जाते हैं क्योंकि उनका Scope समाप्त हो जाता है।

5. Order of Destruction

Objects are destroyed in the reverse order of their construction. Therefore, l4 is destroyed first, followed by l3 and then l2.

Objects का Destruction उनके Construction के विपरीत क्रम (Reverse Order) में होता है। इसलिए सबसे पहले l4, फिर l3 और अंत में l2 Destroy होते हैं।

After Block1 ends, the program returns to the main() function block. The object l1 is destroyed when the main() function finishes.

Block1 समाप्त होने के बाद प्रोग्राम वापस main() Function के Block में आ जाता है। जब main() Function का Execution समाप्त होता है, तब Object l1 Destroy हो जाता है।

Important Points (महत्वपूर्ण बिंदु)

  1. A block is enclosed within curly braces { }.

    Block को Curly Braces { } के अंदर लिखा जाता है।

  2. A block may contain zero or more statements.

    Block में शून्य या उससे अधिक Statements हो सकते हैं।

  3. A block is also known as a compound statement.

    Block को Compound Statement भी कहा जाता है।

  4. Variables and objects declared inside a block generally have block scope.

    Block के अंदर Declare किए गए Variables और Objects का सामान्यतः Block Scope होता है।

  5. Local objects are automatically destroyed when their block is exited.

    Local Objects का Block समाप्त होने पर वे अपने आप Destroy हो जाते हैं।

  6. Objects are destroyed in the reverse order of their construction.

    Objects का Destruction उनके निर्माण के विपरीत क्रम में होता है।

A block is a group of statements enclosed within curly braces { }. It helps organize program statements and defines the scope and lifetime of local variables and objects. In C++, local objects are automatically destroyed when their block is exited.

Block Curly Braces { } के अंदर लिखे गए Statements का समूह होता है। यह प्रोग्राम के Statements को व्यवस्थित करने तथा Local Variables और Objects के Scope एवं Lifetime को निर्धारित करने में सहायता करता है। C++ में Local Objects का Block समाप्त होने पर वे अपने आप Destroy हो जाते हैं।

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)