well , पिछले topic में आपने Array के बारे में पढ़ा और समझा। Array के though हम multiple values को single variable में store करते थे। एक तरह से कई values का group बनाकर एक single variable में assign करते थे। इसके अलावा C++ में struct (structure) functionality भी provide की है।


struct (Structures) कई variables का एक group है , means हम अलग अलग variables की grouping करके एक single variable में store करते हैं जिसे struct कहते हैं। struct में present हर एक variable की member कहते हैं।

C++ create a structure

structure create करने के लिए struct keyword का use किया जाता है , और सभी variables को curly braces {} के अंदर semicolon separated करके लिखा जाता है। इसमें आप different - different types के variables को भी group कर सकते हैं।

struct{
  string name = "Rahul Kumar";
  int age = 26;
  float marks = 56.67;
}user;

example में एक user name का structure define किया गया है , जिसमे 3 members हैं। हालाँकि define करते समय value initialization न करें तो भी कोई problem नहीं यह optional है।

Note : structure को आप कुछ कुछ JavaScript Object से relate कर सकते हैं , हालाँकि यह पूरी तरह से same नहीं है।

C++ access structure members

structure members को आप struct variable के साथ dot syntax . का use करके member को access कर सकते हैं।

C++ structure Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  struct {
    string name = "Rahul Kumar";
    int age = 26;
    float marks = 56.67;
  } user;
  
  // access structure members.
  cout << "Name : " << user.name << endl;
  cout << "Age : " << user.age << endl;
  cout << "Marks : " << user.marks;
  return 0;
}
Output
Name : Rahul Kumar
Age : 26
Marks : 56.67

Note - Example में endl का use line break करने के लिए किया गया है।

C++ Define One Structure in Multiple Variables

structure name define करते समय comm separated , कई name देने से same structure कई variables में define हो जाता है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  struct {
    string name = "Rahul Kumar";
    int age = 26;
    float marks = 56.67;
  } user1, user2;
  
  // change user2 name only
  user2.name = "Ravi";
  
  // user1 data
  cout << "Name : " << user1.name << endl;
  cout << "Age : " << user1.age << endl;
  cout << "Marks : " << user1.marks << endl;
  
  // user2 data
  cout << "Name : " << user2.name << endl;
  cout << "Age : " << user2.age << endl;
  cout << "Marks : " << user2.marks;
  return 0;
}
Output
Name : Rahul Kumar
Age : 26
Marks : 56.67

ध्यान रहे structure value सभी variables में अलग अलग manage होगी। ऐसा नहीं होगा कि एक variable में change होने से सभी जगह change होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers