copy structure in C++ and nested structure in C++

copy structure in C++ Hindi

जैसे कि हम जानते हैं कि हम एकstructure के एक से अधिक structure-variable declare किये जा सकते हैं। वैसे ही , हम किसी भी structure-variable के values को अन्य structure -variable में कॉपी कर सकते हैं, लेकिन याद रखें कि दोनों variable एक ही structure के हों।

चलिए इसे program से समझते हैं।

program में, हमने दो variable vr1 और vr2 declare किए हैं, यहाँ पर हमने केवल variable vr1 को ही initialize किया है,

book vr1={11,"C++ tutorial",230.50}; 
book vr2;

Here we are copying the value of variable vr1 to the variable vr2,like

यहां हम variable vr1 की values को variable vr2 में copy कर रहे हैं-

vr1= vr2;

Here is the program,इसका program नीचे दिया गया है –

copy structure program in C++

#include<iostream.h>
#include<conio.h>

struct book {
    int sr;
    char name[20];
    float price;
  };

book vr1={ 
     11,
     "C++ Tutorial",
     230.50 
  }; // first structure variable initialization 

book vr2; //second structure variable declaration

void main()
 {
   clrscr();
   // accessing values using first variable vr1
   cout<<"Serial No.: ";
   cout<<vr1.sr;
   cout<<"\nBook Name : ";
   cout<<vr1.name;
   cout<<"\nBook Price: ";
   cout<<vr1.price;

   cout<<"\nCopying values to other variable";

      vr2 = vr1; // copying value of vr1 to vr2

 // accessing values using first variable vr2
   cout<<"\nSerial No.: ";
   cout<<vr2.sr;
   cout<<"\nBook Name : ";
   cout<<vr2.name;
   cout<<"\nBook Price: ";
   cout<<vr2.price;
 
   getch();
 }

OUTPUT

Serial No.: 11
Book Name : C++ Tutorial
Book Price: 230.5

Copying values to other variable

Serial No.: 11
Book Name : C++ Tutorial
Book Price: 230.5

nested structure in C++ hindi

nested if -else की तरह C++ nested structure को support करता है इसमें एक structure दूसरे structure का member होता है। ,

यहाँ पर ध्यान दे इसमें पहले structure का structure-variable दूसरे structure का member होता हैं।

सबसे पहले एक first structure को declare करते हैं –

struct first-structure-name { data-members; };

then declare first structure-variable inside to second structure using first structure-variable,such as-

तब पहले structure के variable का declaration दूसरे structure के member के रूप में करते हैं। जैसे-

struct {     //second structure 
   //      first structure variable declaration inside the second structure
      data -member ,
      structure-name variable-name; // first structure variable declaration
};

Here, data-member is the first structure member

Here you can see, in below,

यहां, data -member second structure के members को दर्शाता है।

यहाँ इसका program दिया गया है –

struct first  // first structure
   { 
      int sr;
   };

 struct // second structure
   {
     first s_r;  // accessing first structure using their variable s_r;
     char name[20];
     float price;
   } vr;

accessing nested structure member

both structure members are accessed by the structure-variable of both. The outer-structure is used with a dot operator while the inner-structure is used twice with dot operator (first for outer-structure variable and second with inner-structure variable).

for inner structure member

दोनों structure members को दोनों के structure -variable से access किया जायेगा। केवल first structure variable दो structure – variable से access होगा। पहला first structure के variable से दूसरा second structure के variable से। क्योंकि यह दोनों का member है इसलिए दोनों के variables से access किया जायेगा।

इसका syntax इस प्रकार होगा –

second structure-variable.first-structure-variable.first structure-member name;

जबकि second structure के members साधारण तरीके से ही access होंगे।

second structure-variable.second structure-member;

 vr.name;
 vr.price;

नीचे दिए गए program में, हमने first structure में serial number (sr ) declare किया है,

जबकि अन्य detail (name , book ) second structure में declare किए गए हैं।

जब program को execute किया जाता है, तो book की serial number (sr ) first structure में store होगा जबकि इसे second structure variable vr से access किया जाएगा क्योंकि यह इसका एक member है-

nested structure program in C++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

struct first{    // structure declare
  int sr;
}

struct {
   first s_r;  // assigning first structure into second structure using first structure variable s_r
   char name[20];
   float price;
} vr;

void main()
{
  clrscr();
  cout<<“Enter Serial No.: “;
  cin>>vr.s_r.sr;         // read first structure 
  cout<<“Enter Book Name : “;
  gets(vr.name);
  cout<<“Enter Book Price: “;
  cin>>vr.price;

  cout<<“\nSerial No.: “<<vr.s_r.sr; // first structure member accessing
  cout<<“\nBook Name : “<<vr.name;
  cout<<“\nBook Price: “<<vr.price;
  getch();
}

OUTPUT

Enter Serial No.: 101
Enter Book Name : Programming
Enter Book Price: 350

Serial No.: 101
Book Name : Programming
Book Price: 350

related exercise:

previous-structure in C++

next-union in C++

 

Leave a Reply

Your email address will not be published.