C++ में Structure क्या है प्रोग्राम सहित सम्पूर्ण जानकारी

What is Structure in C++ in Hindi - C++ में Structure क्या है

C++ में एक Structure, एक user- defined Data type है जो विभिन्न Data types वाले Variables को एक साथ एक ही नाम के अंर्तगत रखने की अनुमति देते हैं।

सरल शब्दों मे कहे तो एक Structure एक Container की तरह होते हैं जहां पर आप विभिन्न प्रकार की सूचनाओं को एक साथ रख सकते हैं और उसे एक नाम दे सकते हैं।

Structure उपयोगी होती है क्योंकि वे संबंधित जानकारी को अच्छी तरह से व्यवस्थित करने में मदद करती है जिससे उनके साथ कार्य करना आसान हो जाता है


Structure संबन्धित अन्य महत्पूर्ण बाते

1) Structures को 'struct' keyword तथा उसके बाद structure का नाम का Use करके Define किया जाता है

2) Structure के अंदर विभिन्न प्रकार के Data types वाले Variables को Declare किया जाता है जिन्हे Member कहा जाता है इन Members को Dot '.' Operator का उपयोग करके Access
किया जाता है

3) आप Structure variables को Declare करते समय curly braces '{}' का Use करके Intialize कर सकते है

4) किसी Structure के लिए Memory को एक Contiguous block में allocate किया जाता है जिसमे प्रत्येक Member के लिए एक अलग Memory location होता है 

5) Structure को एक function arguments के रूप मे value या reference के द्वारा Pass किया जाता है

6) C++ में Structure, Class के समान होती है लेकिन इसका Default access member Public पर Set किया जाता है जबकि Class का Default access member Private पर Set किया जाता है

7) Member variables को उसी क्रम में Initialize करना है जिस क्रम मे उसे Declare किए थे

Declaration of Structure in C++ in Hindi 

C++ 'struct' Keyword का Use करके  एक Structure को Program में Use करने से पहले उसे निम्न प्रकार से Declare किया जाता है।

Syntax:
struct structure_name {
   dataType member_name1;
   dataType member_name2;
    ............
    ............
};
struct:
ऊपर Syntax में 'struct' एक keyword है जिसका प्रयोग एक Structute को Define करने में किया जाता है

structure_name:
यह structure को दिया गया नाम है
curly braces'{}' के अंदर Variable के members की उसके Data type के साथ सूची है 

Structure को Declare करने के कुछ Examples

Example 1:
struct Employee
 {
 EID int;
 string name;
 double salary;
 };
ऊपर के कोड मे एक structure `Employee` नाम से Define किया गया है जो अपने में 3 members EID, name, और salary को रखता है।

Example 2:
 struct Student 
   {
    int SID
   string name;
     int age;
  };

Access Structure Members in C++ in Hindi - C++ में Structure member को Access करना

C++ में Dot (.) operator का Use करके Structure के members को Access किया जा सकता है

आप Structures के लिए Pointers का उपयोग करके Structure के Members को भी Access कर सकते है इसके लिए Dot operator के बदले arrow operator (`->`) का Use pointers के साथ करके आप structure members को Access कर सकते हैं

Dot (.) operator का Use करके Structure के members को Access करना


Syntax: structure_variable.member_name

Program for accessing member by Dot operator(.)
#include<iostream> 
#include<cstring> 
using namespace std;
// Define the structure
struct student
 {
    int sid;
    char name[30];
     int age;
};

int main() 
{
    // Declare a variable of type struct employee
    struct student s1, s2 ;

//Storing the first student's Information
    s1.sid = 10; 
    strcpy(s1.name, "Rahul");
    s1.age = 25;
   
//Storing the second student's Information

s2.sid = 12;
strcpy(s2.name, "Aman");
s2.age = 30;

// Printing the first student's Information
cout<<"First student's ID : "<<s1.sid<<endl;
cout<<"First student's name: "<<s1.name<<endl;
cout<<"First student's age: "<<s1.age<<endl<<endl;
 // Printing the second student's Information
cout<<"Second student's ID : "<< s2.sid<<endl;
cout<<"Second student's name: "<<s2.name<<endl;
cout<<"Second student's age: "<<s2.age<<endl;
return 0;
}
Output:
First student's ID : 10 
First student's name: Rahul 
First student's age: 25

Second student's ID : 12
Second student's name: Aman 
Second student's age: 30

Explaination:
ऊपर Program मे एक structure `student` नाम से define किया गया है जिसके members क्रमश: sid, name, और age है

उसके बाद दो variables `s1` और `s2` declare किया गया है
जिसके द्वारा दो Students की Informations जैसे student ID, name, और age को इन variables की मदद से Value Assign किया जाता है

अब cout का प्रयोग करके दोनों students(s1 और s2) की `sid`, `name`, और `age` Print की जाती है  

Pointers का Use करके

आप Structures के लिए Pointers का उपयोग करके Structure के Members को भी Access कर सकते है इसके लिए Dot operator के बदले arrow operator (`->`) का Use pointers के साथ करके आप structure members को Access कर सकते हैं।

Program for accessing member by Pointer
#include<iostream> 
#include<cstring>
using namespace std;
// Define the structure
struct employee 
{
    int id;
    char name[30];
    double salary;
};

int main() 
 {
    struct employee e1, e2;
    struct employee *ptrE1 = &e1;
    struct employee *ptrE2 = &e2;

// Storing information for the first employee
    ptrE1->id = 10;
    strcpy(ptrE1->name, "Amit Kumar");
    ptrE1->salary = 40000;

// Storing information for the second employee
    ptrE2->id = 11;
    strcpy(ptrE2->name, "Yash Kumar");
    ptrE2->salary = 35000;

// Printing information for the first employee
    cout<<"Ist Employee's ID: "<<ptrE1->id<<endl;
    cout<<"Ist Employee's Name: "<<ptrE1->name<<endl;
cout<<"Ist Employee's Salary: "<<ptrE1->salary<<endl<<endl;
// Printing information for the second employee
cout<<"IInd Employee's ID: "<<ptrE2->id<<endl;
cout<<"IInd Employee's Name: "<<ptrE2->name<<endl;
cout<<"IInd Employee's Salary: "<<ptrE2->salary<<endl;

return 0;
}

Output:
Ist Employee's ID: 10
Ist Employee's Name: Amit Kumar
Ist Employee's Salary: 40000

IInd Employee's ID: 11
IInd Employee's Name: Yash Kumar
IInd Employee's Salary: 35000

ऊपर Program में हमने Dot operator के बदले Pointer का Use किया है

Array of Structure in C++ in Hindi

C++ में Structure का एक array आपको एक Structure की कई Copies बनाने और उसे एक array के row के अंदर रखने की अनुमति देता है

यह उस समय मददगार साबित होता है
जब आप एक ही प्रकार के Data elements के Collection के साथ कार्य कर रहे हैं
और प्रत्येक element structure द्वारा represent किया जाता है
है

Program for Array of Structure in C++ 
#include<iostream> 
#include<cstring>
using namespace std;
struct student
 {
    int id;
    char name[30];
    int age;
};

int main() 
{
    struct student Stu[4];

    Stu[0].id =101;
    strcpy(Stu[0].name,"Rahul Sinha");
    Stu[0].age = 30;

// Initialize the second student Stu[1].id = 102; strcpy(Stu[1].name, "Sunil Kumar"); Stu[1].age = 25;

// Initialize the third student
    Stu[2].id = 104;
    strcpy(Stu[2].name, "Aman Sahu");
    Stu[2].age =35;
 
// Initialize the fourth student
    Stu[3].id = 105;
    strcpy(Stu[3].name, "Yash Chopra");
    Stu[3].age = 36;

// Printing information for all students
    
    for (int i = 0; i < 4; i++)
     {
        cout<<"Student's ID: "<<Stu[i].id<<endl;
        cout<<"Student's Name: "<<Stu[i].name<<endl;
        cout<<"Student's Age: "<<Stu[i]. age<<endl;
    }
    return 0;
} 

Output:

Student's ID: 101 
Student's Name: Rahul Sinha Student's Age: 30 
Student's ID: 102 
Student's Name: Sunil Kumar Student's Age: 25 
Student's ID: 104 
Student's Name: Aman Sahu 
Student's Age: 35 
Student's ID: 105
Student's Name: Yash Chopra Student's Age: 36

Explanation:
ऊपर के Program में Structures के Array को दिखाया गया है

1) इसमें सबसे पहले student नामक एक Structure का निर्माण किया गया है ताकि उसमे four students की ID, name और age संबन्धित Information को Store किया जा सके

2) struct student type कर `Stu' नामक एक Array जिसका size, 4 हैं, Declare किया जाता है ताकि उसमे four Students का Information Store किया जा सके

3) उसके बाद Program प्रत्येक student के Data को Array Index का Use करके Initialize करता है और एक loop का Use करके सभी Students की Informations को Print किया जाता है

Structure Pointer in C++ in Hindi

C++ में, structure pointer एक ऐसा pointer variable है जो एक structure के Address को रखता है
यह arrow operator (`->`) का Use करके structure के members को सीधे Access की अनुमति देता है

Structure pointers, Memory को Dynamic allocate करने और Data को कुशलतापूर्वक manipulate करने के लिए उपयोगी है    

आप एक structure variable के Address को pointer को Assign करते हुए एक structure pointer का निर्माण कर सकते हैं

आप pointer के द्वारा `ptr->member का Use करते हुए structure members को Access या Modify कर सकते हैं

Structure pointers आपको संपूर्ण Structure की copy किए हुए बिना structures को functions, Pass करने की अनुमति देता है
 
Program for Structure Pointer
#include<iostream> 
#include<cstring>
using namespace std;
struct Student
 {
    int sid;
    string name;
    int age;
};

int main()
 {
  // Declaration of a structure variable
    Student s1;

// Initialize structure members
    s1.sid = 101;
    s1.name = "Rohit Kumar";
    s1.age = 25;

    // Creating a pointer to the structure
    Student *ptrStudent = &s1;

    // Accessing to structure members using pointer

  cout << "SID: " << ptrStudent->sid << endl;
   cout << "Name: " << ptrStudent->name << endl;
   cout << "Age: " << ptrStudent->age << endl;

    return 0;
}
 
Output:
SID: 101
Name: Rohit Kumar
Age: 25

Difference between Structure and Class in C++ in Hindi

C++, में दोनों structures (struct) और classes (class) का Use, Custom data types को Define करने में किया जाता है जो Data members और member functions को रखता है
तौभी दोनों मे कई अंतर है जो निम्न है
 
1) Struct
इसके Members (variables और functions) by default public (कोई भी इसे Access कर सकते हैं) होते है

Class
इसके Member By default Private (केवल Class के भीतर ही Access हो सकते है) होते है

2) Struct:
यदि एक struct अन्य से inherit होता है तब इसका inheritance By default, public होता है

Class:
यदि एक class अन्य से inherit होता है तब इसका inheritance By default, private होता है

3) Struct
struct का Use सामान्यत: सरल Data को एक साथ समुह में रखने के लिए होता है

Class
इसका Use जटिल objects के निर्माण के लिए होता है जिसमे Data और functions 
दोनों हो सकते हैं

4) Struct
इसमें Data को छुपाने (encapsulating)पर कम ध्यान दिया जाता है और Data अक्सर सीधे ही Access किए जाते हैं

Class
इसमें Data डाटा को छुपाने (encapsulating)पर ज्यादा ध्यान दिया जाता है और Data को functions द्वारा Access किए जाते हैं

5) Struct:
यह C और C++ दोनों में समान रूप से कार्य करता है

Class
यह C++ के लिए विशिष्ट होता है और ऐसे विशेषताओं को offer करता है जो C language में उपलब्ध नहीं है

6) Struct:
इसमें `struct`keyword Use किए जाते हैं

Class
इसमें `class' keyword Use किए जाते हैं`

7) Struct
बड़े पैमाने पर object-oriented designs में कभी कभार ही उपयोग किये जाते है

Class
यह Object-oriented programming, के लिए आवश्यक है जो features जैसे inheritance और polymorphism को support करते है


 Related Posts

> C++ क्या है? उसके इतिहास, गुण, उपयोग, फायदे और नुकसान 

> Basic structure of C++ Program 

> C++ में Tokens क्या है? और उसके प्रकार

> C++ Variables क्या है?, उसके प्रकार, उसे कैसे Declare, Define करते हैं

> C++ में Constants क्या है? उसके प्रकारों की संपूर्ण जानकारी

> C++ में Basic Input और Output (cin,cout,cerr) की जानकारी

> Data type in C++ की संपूर्ण जानकारी 

> C+ में Operators और उसके प्रकार जानें Practical सहित 

> C++ में Conditional और उसके प्रकारों को जानें Practical सहित 

> C++ में Looping statements और उसके प्रकार Practical सहित 

> C++ में Jump Statements और उसके प्रकारों की संपूर्ण जानकारी Practical सहित

> C++ में Array क्या है? और उसके प्रकारों की जानकारी Practical सहित

> C++ में Function क्या है उसके प्रकार, उपयोग प्रोग्राम सहित 

> OOPs Concepts in C++ in Hindi- C++ में OOPs के बारे में

> Oops के फायदे और नुकसान की जानकारी 

> OOP और POP के बीच अंतर 

> C++ में Class और Object की सम्पूर्ण जनकारी

> C++ में Array of Objects क्या है? 

> C++ में Pointers, Pointer to an objects, Pointer to an Array की संपूर्ण जानकारी हिंदी में।

C++ में Passing objects क्या है

> C++ में Reference और Type Casting की संपूर्ण जानकारी

> C++ में Access specifier की संपूर्ण जानकारी 

> C++ में Static Data Members और Member Functions के बारे में Practical सहित 

> C++ में Memory allocation और Memory management operators (new और delete) Practical सहित 

> Friend Function in C++ in Hindi 

> Friend Class in C++ in Hindi Practical सहित 

> Inline function in C++ in Hindi 

> Function Overloading in C++ in Hindi Practical सहित 

> Operator Overloading in C++ in Hindi Practical सहित 

> C++ में Constructor क्या है और उसके प्रकारों की संपूर्ण जानकारी

> C++ में Destructor क्या है ?उसकी संपूर्ण जानकारी

> C++ मे Inheritance क्या है उसके प्रकारों को जानें प्रोग्राम सहित

> C++ में Polymorphism क्या है? और उसके प्रकारों को जानें

> C++ में Virtual function की संपूर्ण जानकारी

> C++ में File handling की संपूर्ण जानकारी 

> C++ में Exception handling  की संपूर्ण जानकारी