Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔵 Core C++  ·  Lesson 28

Structures in C++

Structure क्या है?

Structure संबंधित data के टुकड़ों को — संभवतः अलग types के — एक नाम के तहत समूहित करता है। एक student के नाम, roll number और marks के लिए अलग variables के बजाय, आप उन्हें एक Student value में बाँध देते हैं।

struct परिभाषित करना

C++
struct Student {
    string name;
    int roll;
    double marks;
};   // semicolon dhyan dein

यह Student नामक एक नया type परिभाषित करता है। Members इसके अंदर के variables हैं।

बनाना और access करना

struct type का variable बनाएँ और dot operator से इसके members तक पहुँचें:

C++
#include <iostream>
using namespace std;
struct Student { string name; int roll; double marks; };

int main() {
    Student s;
    s.name = "Amit";
    s.roll = 12;
    s.marks = 88.5;
    cout << s.name << " (" << s.roll << "): " << s.marks << "\n";
    return 0;
}
Output:
Amit (12): 88.5

आप एक साथ भी initialise कर सकते हैं: Student s = {"Amit", 12, 88.5};। Pointer के साथ, arrow इस्तेमाल करें: p->name।

Structs का array

कई records रखना आसान है — struct का array बनाएँ:

C++
Student list[2] = {{"Amit", 1, 90}, {"Riya", 2, 95}};
for (int i = 0; i < 2; i++)
    cout << list[i].name << " " << list[i].marks << "\n";
Dry Run — dekhiye har step kaise chalta hai (i = 0 se shuru)
Stepi ki valueConditionKya huai ab
100 < 2 → trueloop body chalta haii → 1
211 < 2 → trueloop body chalta haii → 2
322 < 2 → falseCondition false — loop yahin ruk gaya (STOP)—
Output:
Amit 90
Riya 95

Functions के साथ structs

Struct को function को भेजें — बड़े को copy से बचाने को const reference द्वारा:

C++
void printStudent(const Student &s) {
    cout << s.name << ": " << s.marks << "\n";
}

struct बनाम class

structclass
Default accesspublicprivate
सामान्य इस्तेमालसादा dataData + व्यवहार
Methods हो सकते हैं?हाँहाँ

एकमात्र भाषा अंतर default access है। व्यवहार और encapsulation के साथ आगे बढ़ने को classes और objects देखें।

आम गलतियाँ

  • struct के closing brace के बाद semicolon भूलना।
  • Pointer पर -> के बजाय . इस्तेमाल करना।
  • बड़े structs को value से copy करना जब const reference बेहतर है।
  • struct members को private मानना — वे default रूप से public हैं।
🏋️ अभ्यास

title, author और price वाला एक Book struct परिभाषित करें। तीन books का array बनाएँ, उन्हें पढ़ें, और सबसे महँगी print करें।

सारांश

  • struct अलग types के संबंधित data को एक नाम के तहत समूहित करता है।
  • Members को dot operator से access करें (या pointer के ज़रिए ->)।
  • Structs के arrays एक ही आकार के कई records रखते हैं।
  • Copies से बचने को structs को functions को const reference द्वारा भेजें।
  • struct और class केवल default access में भिन्न हैं (public बनाम private)।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में structure क्या है?
Structure, struct से घोषित, अलग types के संबंधित variables को एक नाम के तहत समूहित करता है। जैसे एक Student struct एक नाम, roll number और marks को साथ रख सकता है, तो आप एक student को अलग variables के बजाय एक single value की तरह मान सकते हैं।
C++ में struct के members कैसे access करते हैं?
आप struct variable पर dot operator इस्तेमाल करते हैं, जैसे s.name या s.marks। अगर आपके पास struct का pointer हो, आप इसके बजाय arrow operator इस्तेमाल करते हैं, जैसे p->name, जो (*p).name का संक्षिप्त रूप है।
C++ में struct और class में क्या अंतर है?
C++ में struct और class लगभग समान हैं; एकमात्र असली अंतर default access है। struct के members default रूप से public होते हैं, जबकि class के members default रूप से private। परंपरा से, structs सादे data के लिए और classes व्यवहार वाले types के लिए इस्तेमाल होते हैं।
क्या C++ में struct में functions हो सकते हैं?
हाँ। C के विपरीत, C++ struct में data के साथ member functions भी हो सकते हैं, क्योंकि struct और class लगभग एक जैसे हैं। पर व्यवहार में, structs आमतौर पर सादे data holders रखे जाते हैं और classes तब इस्तेमाल होते हैं जब आपको methods और encapsulation चाहिए।
C++ में structures का array कैसे बनाते हैं?
आप ऐसा array घोषित करते हैं जिसका element type struct हो, जैसे Student list[3];, और फिर हर element तथा उसके members को साथ access करते हैं, जैसे list[0].name। यह एक ही आकार के कई records रखने के लिए काम आता है, जैसे students की सूची।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicEnumerations in C++ आपकी प्रगति: 28 / 81 (35%) · Lesson 29 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें