हिंदी माध्यम नोट्स
Categories: c++ language in hindi
C++ : Structure in hindi , write what is structure in c++ language with programs and examples explanation
write what is structure in c++ language with programs and examples explanation , C++ : Structure in hindi :-
इससे पहले के article मे compound data type मे से string को discuss किया गया है | लेकिन अब इस article मे structure data type को discuss करेगे |
Structure
अगर किसी person के address को store करते है | इसमें house number , street name , city और pin-code को store करना होता है | इसमें मे से
house number : जो की integer type है |
street name और city : दोनों ही string है |
pincode : ये integer data type है |
इसके लिए array और string दोनों ही data type उपयुक्त नहीं है | array कई सारे data को store कर सकता है लेकिन सभी data सामान होता है |अतः इसका उदाहरण होता है :
int a[20];
इसमें 20 integer value को store हो सकता है |लेकिन मिक्स datatype store नहीं हो सकता है |
इसके लिए structure data type को use किया जाता है | structure एक एस data type है जिसमे अलग अलग data types के values store होती है |structure data type से address के सभी elements को store किया जा सकता है | जब हमे एक या एक से ज्यादा person की address को store करना होता है तब structure के array को use किया जा सकता है |
structure datatype OOPs का सबसे महतवपूर्ण concept होता है जो की OOPs के concept के implement मे use होता है |
Structure एक यूजर define data type है | Structure data type है जिसके type से दुसरे variable को declare किया गया है | Structure को declare करने के बाद इसके type का variable को declare किया जा सकता है |
किसी Structure को declare करने के लिए दो भाग होते है :
1.Structure name
इसमें Structure के नाम को define किया जाता है | इसके लिए struct keyword को use किया जाता है | इसका syntax होता है :
struct Structure_name {
Structure Description ;
};
2.Structure Description
Structure Description मे Structure के members को define किया जाता है | इसमें Structure मे store होने वाली अलग अलग data type के variable को define किया जाता है | इसका syntax है :-
struct address {
int house ;
char street[20];
char city[20];
int pincode;
};
इस उदाहरण मे , address एक Structure data type है जिसमे चार variables define किया गया है :
int house : इसमें address मे से house number को store किया जाता है |
char street[20]; इसमें address मे से street का नाम store होता है |
char city[20];इसमें address मे से city का नाम store होता है|
int pincode;इसमें address मे से pin code store होता है|
Input और Output in Structure
किसी Structure के member को access करने के लिए memebership operator (.) का use किया जाता है | उदाहरण के लिए
अगर Structure address मे से city को access करना है तो इसका syntax address.city होगा |
अगर Structure address मे से house को access करना है तो इसका syntax address.house होगा |
अगर Structure address मे से pincode को access करना है तो इसका syntax address.pincode होगा |
जब address.house का type integer होता है क्योकि house का type integer होता है | अतः address.city का type string होता है क्योकि city का data type string होता है |
Structure का उदाहरण :
#include<iostream.h>
#include<conio.h>
struct address {
int house ;
char street[20];
char city[20];
int pincode;
};
void main()
{
struct address s;
struct address s;
using namespace std;
cout<< “Enter House Number : “<<endl;
cin>>s.house;
cout<<“Enter Street Name :”<<endl;
cin.get(s.street);
cout<<“Enter City Name :”<<endl;
cin.get(s.city);
cout<<“Enter Pin Code :”<<endl;
cin>>s.pincode;
cout<<“House Number : “<<s.house<<endl;
cout<<“Street Name : “<< s.street<<endl;
cout<<“City Name : “<< s.city <<endl;
cout<<“Pin Code : “<< s.pincode <<endl;
cout<<“City Name : “<< s.city <<endl;
cout<<“Pin Code : “<< s.pincode <<endl;
cout<<“Process Completed ! Forward To Next Form.”<<endl;
getch();
}
इस उदाहरण मे , address एक structure है जिसमे किसी address का निन्म data को add किया जा सकता है |जिसमे से city और street string है लेकिन इसे declare करने के लिए character को use किया है |
House: इसमें address मे से house number store होगा |
street : इसमें street का नाम store होगी |
city : इसमें city की information को store किया जाता है |
इन सभी access करने के लिए membership operator का use किया गया |
इस उदाहरण मे , address एक structure है जिसमे किसी address का निन्म data को add किया जा सकता है |जिसमे से city और street string है लेकिन इसे declare करने के लिए character को use किया है |
House: इसमें address मे से house number store होगा |
street : इसमें street का नाम store होगी |
city : इसमें city की information को store किया जाता है |
इन सभी access करने के लिए membership operator का use किया गया |
इसका आउटपुट होगा :
Enter House Number : 12
Enter Street Name : Ashok Nagar
Enter City : Jaipur
Enter Pincode : 302004
House Number : 12
Street Name : Ashok Nagar
City : Jaipur
Pincode : 302004
Structure with string class
String class को Structure के साथ use किया जा सकता है | लेकिन कभी कभी ये compiler पर भी निर्भर करता है | जैसे Borland c++ 5.5 और Microsoft visual C++ version 7.0 , String class को Structure के साथ use नहीं कर सकता है |
जब किसी string को Structure के साथ use किया जाता है तब std namespace को जरुर use किया जा सकता है | इससे using namespace std ; से बना सकते है | या std:: string; statement से use किया जा सकता है |
उदाहरण के लिए :
Enter House Number : 12
Enter Street Name : Ashok Nagar
Enter City : Jaipur
Enter Pincode : 302004
House Number : 12
Street Name : Ashok Nagar
City : Jaipur
Pincode : 302004
Structure with string class
String class को Structure के साथ use किया जा सकता है | लेकिन कभी कभी ये compiler पर भी निर्भर करता है | जैसे Borland c++ 5.5 और Microsoft visual C++ version 7.0 , String class को Structure के साथ use नहीं कर सकता है |
जब किसी string को Structure के साथ use किया जाता है तब std namespace को जरुर use किया जा सकता है | इससे using namespace std ; से बना सकते है | या std:: string; statement से use किया जा सकता है |
उदाहरण के लिए :
#include<iostream.h>
#include<conio.h>
#include<string>
#include<string>
struct student {
int RollNumber;
std::string name;
std::string name;
std::string class;
int grade;
};
void main()
{
struct student s;
struct student s;
using namespace std;
cout<< “Enter Roll Number : “<<endl;
cin>>s.RollNumber;
cout<<“Enter Student Name :”<<endl;
cin.get(s.name);
cout<<“Enter Class :”<<endl;
cin.get(s.class);
cout<<“Enter Grade :”<<endl;
cin>>s.grade;
cout<<“Roll Number : “<<s.RollNumber<<endl;
cout<<“Name : “<< s.name <<endl;
cout<<“Class : “<< s.class <<endl;
cout<<“Grade : “<< s.grade <<endl;
cout<<“Class : “<< s.class <<endl;
cout<<“Grade : “<< s.grade <<endl;
cout<<“Process Completed ! Forward To Next Form.”<<endl;
getch();
}
इस उदाहरण मे , student एक structure है जिसमे किसी student का निन्म data को add किया जा सकता है |जिसमे से class और name string है |
Roll number: इसमें student का roll number store होगा |
Class : इसमें student की class की information store होगी |
grade : इसमें student के द्वार प्राप्त की गयी grade को store किया जाता है |इन सभी access करने के लिए membership operator का use किया गया |
Roll number: इसमें student का roll number store होगा |
Class : इसमें student की class की information store होगी |
grade : इसमें student के द्वार प्राप्त की गयी grade को store किया जाता है |इन सभी access करने के लिए membership operator का use किया गया |
इसका आउटपुट होगा :
Enter Roll Number : 12
Enter Student Name : Rahul
Enter Class : Fourth
Enter Grade : 81
Roll Number : 12
Name : Rahul
Class : Fourth
Grade : 81
इस article मे ,structure का basic को discuss किया है |अब आगे आने वाले article मे structure के advance feature को discuss करेगे |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago