Structure in C++ in Hindi – C++ में स्ट्रक्चर क्या है?

Structure क्या होता है?

C++ में, Structure एक user-defined data type है जिसका प्रयोग अलग-अलग data types के elements को एक साथ स्टोर करने के लिए किया जाता है।

Structure एक data type को create करता है जिससे कि विभिन्न data types के items को एक साथ एक group में रखा जा सके।

उदाहरण के लिए – माना कि आप किसी person की information को स्टोर करना चाहते हैं. जैसे कि – name, address, age, salary आदि. तो आप इसके लिए variables को create कर सकते हैं और data को अलग स्टोर कर सकते हैं.

परन्तु अगर आप भविष्य में एक से ज्यादा persons की information को स्टोर करना चाहेंगे तो आपको प्रत्येक person के लिए variables को create करना पड़ेगा. इससे हमारा code बहुत ही खराब दिखेगा. इससे बचने के लिए हम structure का प्रयोग करते हैं.

Structure को create कैसे करते हैं?

structure को create करने के लिए struct कीवर्ड का प्रयोग किया जाता है. इसको create करने का सामान्य syntax निम्नलिखित हैं:-

struct struct_name  
{  
     // struct members
}   

उदाहरण के लिए –

struct Person  
{  
char name[30];  
char address[50];  
int age;
float salary;
           
};  

ऊपर दिए गये उदाहरण में Person एक structure है. जिसमें चार members हैं. ये members हैं:- name, address, age, salary. इसमें name और address char डाटा टाइप हैं. age एक int data type है. और salary एक float डाटा टाइप है.

जब एक structure को create किया जाता है तो उसे memory नहीं दी जाती. इसे memory तभी दी जाती है जब इसमें सभी variables को add कर लिया जाता है.

इसे पढ़ें:Class और Structure के बीच अंतर क्या है?

Structure के members को कैसे access करते हैं?

Structure members को dot(.) operator का प्रयोग करके access करते हैं:-

#include <iostream>    
using namespace std;
struct Person
{
char name[50] ;
int age;
};
int main(void) {
// create structure instance
struct Person p;
//access array members
p.name = “Deepak”;
p.age = 25;
cout << "Person name: " << p.name << endl;
cout << "Person age: " << p.age << endl;

return 0;
}

इसका आउटपुट:-
Person name: Deepak
Person age: 25

Structure pointer क्या होता है?

एक ऐसे pointer को create किया जा सकता है जो structure को point करता हो. यदि हमारे पास pointer to structure होता है तो members को access करने के लिए dot operator की बजाय arrow (->) operator का प्रयोग किया जाता है.

#include <iostream> 
using namespace std; 
  
struct Point { 
int x, y; 
}; 
  
int main() 
{ 
struct Point p1 = { 1, 2 }; 
  
// p2 is a pointer to structure p1 
struct Point* p2 = &p1; 
  
// Accessing structure members using 
// structure pointer 
cout << p2->x << " " << p2->y; 
return 0; 
}

इसका आउटपुट –
1 2

structure का प्रयोग function argument के रूप में

हम structure को function में argument के रूप में pass कर सकते हैं. यह उसी तरह किया जाता है जैसे हम किसी सामान्य argument को pass करते हैं.

इसका example –

#include <iostream>
using namespace std;

struct Person
{
    char name[50];
    int age;
    float salary;
};

void displayData(Person);   // Function declaration

int main()
{
    Person p;

    cout << "Enter Full name: ";
    cin.get(p.name, 50);
    cout << "Enter age: ";
    cin >> p.age;
    cout << "Enter salary: ";
    cin >> p.salary;

    // Function call with structure variable as an argument
    displayData(p);

    return 0;
}

void displayData(Person p)
{
    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}

Limitations of Structure in C++ in Hindi – स्ट्रक्चर की कमियां

इसकी कमियां निम्नलिखित हैं:-

  • struct data type को built in data type की तरह treat नहीं किया जा सकता.
  • structure variables में operators का इस्तेमाल नहीं किया जा सकता.
  • स्ट्रक्चर data hiding को support नहीं करता है. स्ट्रक्चर के members को किसी function के द्वारा भी access किया जा सकता है.
  • static members को structure body के अंदर declare नहीं किया जा सकता.
  • स्ट्रक्चर के अंदर constructors को create नहीं किया जा सकता है.

References:- https://www.geeksforgeeks.org/structures-in-cpp/

निवेदन:- यदि आपके लिए यह पोस्ट उपयोगी रहा हो तो इसे अपने दोस्तों के साथ अवश्य share कीजिये. और आपके c++ या किसी अन्य subjects से सम्बन्धित कोई questions हो तो उसे नीचे comment के माध्यम से बताइए. Thanks.

Summary (सारांश)

C++ में Structure एक user-defined data type है जो विभिन्न प्रकार के data items को एक साथ group करके store करने का तरीका सिखाता है। यह article पूरी तरह से समझाता है कि Structure कैसे काम करता है और इसे क्यों उपयोग करें। इसमें आप सीखेंगे कि struct कीवर्ड का उपयोग करके कैसे एक नया data type बनाया जाता है, जिसमें अलग-अलग data types (जैसे int, float, char array) को एक साथ रखा जा सकता है। उदाहरण के लिए, एक Person structure में name, address, age और salary जैसे members को शामिल करना समझाया गया है। इस लेख को पढ़ने के बाद छात्र निम्नलिखित सीखेंगे:

  • Structure को define और declare करने का सही syntax।
  • Dot (.) operator के द्वारा structure members को access करना।
  • Structure बनाम normal variables का उपयोग कब करना चाहिए, ताकि code अधिक organized और reusable बने।
  • Structure और Class के बीच मुख्य अंतर को समझना।
यह article Exam preparation के लिए बहुत मददगार है, क्योंकि इसमें real-life example (जैसे कई persons की information store करना) दिया गया है, जिससे concepts clear होते हैं और coding में confidence बढ़ता है।

Leave a Comment