Defining and Using Structures

How to group different types of data together under one name.

Defining and Using Structures

Structure एक user-defined data type (उपयोगकर्ता द्वारा परिभाषित डेटा प्रकार) है जिसका उपयोग अलग-अलग data types के related data को एक ही unit में store करने के लिए किया जाता है। C++ में structure को struct keyword की सहायता से define किया जाता है।

उदाहरण के लिए, किसी student की information में उसका roll number, name और marks हो सकते हैं। इन सभी values के लिए अलग-अलग variables बनाने के बजाय structure की सहायता से इन्हें एक ही unit में रखा जा सकता है।

Structure: Structure एक user-defined data type है जो different data types के related variables को एक single unit में group करता है।

Need for Structures

किसी program में यदि किसी object या entity से संबंधित कई प्रकार के data को store करना हो, तो सामान्य variables का उपयोग करने पर program को manage करना कठिन हो सकता है। Structure related data items को एक logical unit में organize करता है।

मान लीजिए हमें किसी student की information store करनी है:

  • Roll Number → int
  • Name → string
  • Marks → float

इन अलग-अलग data types को एक ही structure में रखा जा सकता है।

Defining a Structure

C++ में structure को struct keyword का उपयोग करके define किया जाता है। Structure definition में structure का नाम और उसके members (सदस्य) लिखे जाते हैं।

Structure का सामान्य syntax इस प्रकार है:

struct StructureName
{
    data_type member1;
    data_type member2;
    data_type member3;
};

उदाहरण के लिए, student की information के लिए structure इस प्रकार define किया जा सकता है:

struct Student
{
    int rollNo;
    string name;
    float marks;
};

यहाँ Student structure का नाम है और rollNo, name तथा marks इसके members हैं।

Important: Structure definition के अंत में semicolon ; लगाना आवश्यक है।

Structure Members

Structure के अंदर declare किए गए variables को members कहा जाता है। Structure के members अलग-अलग data types के हो सकते हैं।

Member Data Type Purpose
rollNo int Student का roll number store करना
name string Student का नाम store करना
marks float Marks store करना

Creating Structure Variables

Structure define करने के बाद उसके type के variables बनाए जा सकते हैं। ऐसे variables को structure variables कहा जाता है।

उदाहरण:

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s1;
    return 0;
}

यहाँ s1 एक structure variable है जिसका type Student है।

Accessing Structure Members

Structure variable के members को access करने के लिए dot (.) operator का उपयोग किया जाता है।

इसका सामान्य syntax है:

structure_variable.member_name;

उदाहरण:

s1.rollNo
s1.name
s1.marks

यहाँ s1 structure variable है और dot operator की सहायता से उसके individual members को access किया जा रहा है।

Assigning Values to Structure Members

Structure के members को value assign करने के लिए dot operator का उपयोग किया जाता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s1;

    s1.rollNo = 101;
    s1.name = "Amit";
    s1.marks = 85.5;

    cout << "Roll No: " << s1.rollNo << endl;
    cout << "Name: " << s1.name << endl;
    cout << "Marks: " << s1.marks;

    return 0;
}
Output:
Roll No: 101
Name: Amit
Marks: 85.5

Initializing a Structure Variable

Structure variable को declaration के समय भी initialize किया जा सकता है। Members की values को braces { } के अंदर क्रम से दिया जाता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s1 = {101, "Ravi", 92.5};

    cout << s1.rollNo << endl;
    cout << s1.name << endl;
    cout << s1.marks;

    return 0;
}
Output:
101
Ravi
92.5
याद रखें: Initialization में values उसी order में दी जाती हैं जिस order में structure के members declared हैं।

Taking Input in a Structure

Structure members में user से input भी लिया जा सकता है। प्रत्येक member को dot operator की सहायता से access करके input लिया जाता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s1;

    cout << "Enter roll number: ";
    cin >> s1.rollNo;

    cout << "Enter name: ";
    cin >> s1.name;

    cout << "Enter marks: ";
    cin >> s1.marks;

    cout << "\nStudent Details\n";
    cout << "Roll No: " << s1.rollNo << endl;
    cout << "Name: " << s1.name << endl;
    cout << "Marks: " << s1.marks;

    return 0;
}
Sample Output:
Enter roll number: 102
Enter name: Neha
Enter marks: 88.5

Student Details
Roll No: 102
Name: Neha
Marks: 88.5

Array of Structures

यदि हमें एक से अधिक students की information store करनी हो, तो structure variables की array बनाई जा सकती है। इसे Array of Structures कहा जाता है।

उदाहरण:

Student students[3];

यहाँ students नाम की array में तीन Student structure variables store किए जा सकते हैं।

Example of Array of Structures

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s[3] = {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    for (int i = 0; i < 3; i++)
    {
        cout << s[i].rollNo << " "
             << s[i].name << " "
             << s[i].marks << endl;
    }

    return 0;
}
Output:
101 Amit 85.5
102 Neha 91
103 Ravi 78.5

यहाँ s[i] एक structure variable को represent करता है और s[i].member के द्वारा उसके members को access किया गया है।

Structure with Different Data Types

Structure की सबसे महत्वपूर्ण विशेषता यह है कि इसमें different data types के members को एक साथ रखा जा सकता है।

struct Employee
{
    int id;
    string name;
    float salary;
    char grade;
};

इस structure में int, string, float और char सभी data types के members हैं।

Member Type
id int
name string
salary float
grade char

Structure as a User-Defined Data Type

Structure को user-defined data type कहा जाता है क्योंकि programmer स्वयं structure के members और उनके data types को define करता है। Structure define करने के बाद उसका नाम एक नए data type की तरह उपयोग किया जा सकता है।

उदाहरण:

struct Book
{
    int price;
    string title;
};

Book b1;
Book b2;

यहाँ Book एक user-defined type है और b1 तथा b2 इसके variables हैं।

Structure and Class in C++

C++ में structure और class दोनों user-defined types हैं और दोनों में data members तथा member functions हो सकते हैं। इनके बीच एक महत्वपूर्ण difference default access control का है।

Structure Class
Default access specifier public होता है। Default access specifier private होता है।
struct keyword का उपयोग होता है। class keyword का उपयोग होता है।
Data members default रूप से public होते हैं। Data members default रूप से private होते हैं।
Exam Point: C++ में structure के members default रूप से public होते हैं, जबकि class के members default रूप से private होते हैं।

Advantages of Structures

  • Related data को एक single unit में organize किया जा सकता है।
  • Different data types के members को एक structure में रखा जा सकता है।
  • Program को अधिक organized और readable बनाया जा सकता है।
  • एक ही structure type के कई variables बनाए जा सकते हैं।
  • Array of structures की सहायता से multiple records store किए जा सकते हैं।
  • Real-world entities जैसे Student, Employee, Book आदि को represent करना आसान होता है।

Real-Life Example of Structure

मान लीजिए किसी library में books की information store करनी है। प्रत्येक book के लिए book ID, title और price की आवश्यकता है। Structure की सहायता से इन तीनों values को एक unit में रखा जा सकता है।

#include <iostream>
#include <string>
using namespace std;

struct Book
{
    int id;
    string title;
    float price;
};

int main()
{
    Book b1 = {1, "C++ Programming", 450.0};

    cout << "Book ID: " << b1.id << endl;
    cout << "Title: " << b1.title << endl;
    cout << "Price: " << b1.price;

    return 0;
}
Output:
Book ID: 1
Title: C++ Programming
Price: 450

Important Points

  • Structure एक user-defined data type है।
  • Structure को define करने के लिए struct keyword का उपयोग किया जाता है।
  • Structure के अंदर declared variables को members कहा जाता है।
  • Structure में different data types के members हो सकते हैं।
  • Structure variable के members को access करने के लिए dot (.) operator का उपयोग होता है।
  • Structure definition के अंत में semicolon लगाना आवश्यक है।
  • एक ही structure type के कई variables बनाए जा सकते हैं।
  • Multiple records store करने के लिए array of structures का उपयोग किया जा सकता है।
  • C++ में structure के members default रूप से public होते हैं।
  • Structure real-world entities को represent करने के लिए उपयोगी है।

Board Focus

Exam के लिए याद रखें:
Structure → User-defined data type
Keyword → struct
Members → Structure के अंदर declared variables
Member Access → Dot (.) operator
Structure Definition → अंत में semicolon (;)
Different Data Types → एक structure में संभव
Multiple Records → Array of Structures
Default Access → public

Board Important Questions

Very Short Answer Questions

Q1. Structure क्या है?

Answer: Structure एक user-defined data type है जो related data of different types को एक single unit में group करता है।

Q2. C++ में structure define करने के लिए किस keyword का उपयोग किया जाता है?

Answer: struct keyword.

Q3. Structure के अंदर declared variables को क्या कहा जाता है?

Answer: Members.

Q4. Structure variable के members को access करने के लिए किस operator का उपयोग होता है?

Answer: Dot (.) operator.

Q5. क्या structure में different data types के members हो सकते हैं?

Answer: हाँ, structure में different data types के members हो सकते हैं।

Q6. C++ structure के members का default access specifier क्या होता है?

Answer: Public.

Short Answer Questions

Q7. Structure को उदाहरण सहित समझाइए।

Answer: Structure एक user-defined data type है जिसका उपयोग different data types के related data को एक single unit में store करने के लिए किया जाता है। उदाहरण के लिए:

struct Student
{
    int rollNo;
    string name;
    float marks;
};

इस structure में rollNo, name और marks तीन members हैं।

Q8. Structure member को कैसे access किया जाता है?

Answer: Structure variable के member को dot (.) operator की सहायता से access किया जाता है। उदाहरण:

Student s1;
s1.rollNo = 101;
s1.marks = 90.5;

Q9. Array of Structures क्या है?

Answer: जब एक ही structure type के multiple variables को array के रूप में store किया जाता है, तो उसे Array of Structures कहा जाता है। उदाहरण:

Student s[50];

यहाँ 50 Student records store किए जा सकते हैं।

Q10. Structure और class में एक प्रमुख अंतर बताइए।

Answer: C++ में structure के members default रूप से public होते हैं, जबकि class के members default रूप से private होते हैं।

Long Answer Questions

Q11. C++ में structure define करने और उसका उपयोग करने की प्रक्रिया समझाइए।

Answer: C++ में structure को struct keyword की सहायता से define किया जाता है। इसमें related data के members declare किए जाते हैं। Structure define करने के बाद उसके type का variable बनाया जाता है और dot operator की सहायता से उसके members को access किया जाता है। उदाहरण:

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s1;

    s1.rollNo = 101;
    s1.name = "Ravi";
    s1.marks = 89.5;

    cout << s1.rollNo << endl;
    cout << s1.name << endl;
    cout << s1.marks;

    return 0;
}
Output:
101
Ravi
89.5

Q12. Array of Structures को उदाहरण सहित समझाइए।

Answer: जब एक ही structure type के कई records को array के रूप में store किया जाता है, तो इसे Array of Structures कहते हैं। इसका उपयोग students, employees, books आदि के multiple records store करने के लिए किया जाता है। उदाहरण:

struct Student
{
    int rollNo;
    string name;
    float marks;
};

Student s[3];

यहाँ s array में तीन Student records store किए जा सकते हैं और individual members को s[0].rollNo, s[1].name आदि के रूप में access किया जा सकता है।

Q13. Structure के advantages लिखिए।

Answer: Structure related data को एक unit में organize करता है। इसमें different data types के members रखे जा सकते हैं। यह program को readable और manageable बनाता है। Structure की सहायता से real-world entities को represent करना आसान होता है और array of structures के माध्यम से multiple records को efficiently organize किया जा सकता है।

Quick Revision

  • Structure: User-defined data type
  • Keyword: struct
  • Members: Structure के अंदर declared variables
  • Member Access: Dot (.) operator
  • Structure Variable: Structure type का variable
  • Array of Structures: एक ही structure type के multiple records
  • Default Access: public
  • Definition: अंत में semicolon आवश्यक है
One-Line Revision: C++ में structure एक user-defined data type है जो different data types के related members को एक single unit में group करता है और उसके members को dot (.) operator द्वारा access किया जाता है।

Practice Questions

  1. Structure क्या है?
  2. Structure define करने के लिए कौन-सा keyword उपयोग किया जाता है?
  3. Structure members क्या होते हैं?
  4. Structure variable के members को कैसे access किया जाता है?
  5. Structure definition का general syntax लिखिए।
  6. Structure में different data types का उपयोग कैसे किया जाता है?
  7. Structure variable को initialize करने का उदाहरण दीजिए।
  8. Array of Structures क्या है?
  9. Structure और class में अंतर बताइए।
  10. Student की information store करने के लिए एक structure define कीजिए।
  11. Book की information store करने के लिए एक structure बनाइए।
  12. Array of Structures का उपयोग करते हुए तीन students के records store करने का program लिखिए।
  13. Structure के advantages लिखिए।
  14. Structure members को input और output करने का C++ program लिखिए।
Lesson 1 of 37
On This Page