C++ के Structures
In this page:
struct StructName {
data_type member1;
data_type member2;
};
StructName variable_name = {value1, value2};
variable_name.member1;
Struct क्या है?
C++ में एक struct possibly अलग types के related variables को एक नाम के तहत group करता है, आपको एक single unit देते हुए जो एक real-world record model करता है जैसे एक point, एक date, या एक book।
एक plain array के उलट, हर member का अपना नाम और type होता है, इसलिए आप fields को numeric index की बजाय meaning से access करते हैं (book.pages)।
उदाहरण: What Is a Struct?
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point {
int x;
int y;
};
// Program execution starts in main()
int main() {
Point p = {3, 4};
// Print to the console with cout
std::cout << p.x << "," << p.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structs Declare और इस्तेमाल करना
आप struct keyword से एक नाम के बाद member variables की एक brace-enclosed list के साथ एक struct declare करते हैं, फिर instances उसी तरह बनाते हैं जैसे आप किसी दूसरे type को declare करते हैं।
Dot notation (.) एक struct value पर एक member तक पहुँचता है, जबकि arrow notation (->) एक struct के pointer के जरिए एक member तक पहुँचता है।
उदाहरण: Declaring and Using Structs
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point {
int x;
int y;
};
// Program execution starts in main()
int main() {
Point p1;
p1.x = 1;
p1.y = 2;
// Print to the console with cout
std::cout << p1.x << "," << p1.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
C++ में Structs बनाम Classes
C के उलट, एक C++ struct में constructors, member functions, और यहाँ तक कि inheritance हो सकते हैं -- एक class से असली फर्क सिर्फ यह है कि struct members default से public हैं जबकि class members default से private हैं।
Convention अभी भी simple data-holding types के लिए struct और real behavior और protect करने के लिए invariants वाले types के लिए class इस्तेमाल करता है, भले ही compiler उन्हें लगभग identically treat करे।
उदाहरण: Structs vs Classes in C++
#include <iostream>
struct Point {
int x;
Point(int px) : x(px) {} // structs can have constructors too
};
int main() {
Point p(5); // only real difference from class: public by default
std::cout << p.x << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structs का Nesting और Arrays
Structs दूसरे structs को members की तरह रख सकते हैं, आपको एक Person के अंदर एक Address जैसे composite types build करने देते हुए, और आप same shape के कई records एक contiguous block में रखने के लिए structs के arrays declare कर सकते हैं।
यह same pattern है जो एक पूरी class hierarchy तक पहुँचने से पहले tabular data की rows model करने के लिए इस्तेमाल होता है।
उदाहरण: Nesting and Arrays of Structs
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Define a structure type named Address
struct Address { std::string city; };
// Define a structure type named Person
struct Person { std::string name; Address addr; };
// Program execution starts in main()
int main() {
Person p = {"Alex", {"Delhi"}};
// Print to the console with cout
std::cout << p.name << " lives in " << p.addr.city << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions को Structs Pass करना
एक struct को value से pass करना हर member copy करता है, जो बड़े structs के लिए wasteful है, इसलिए functions आमतौर पर copy किए बिना data पढ़ने के लिए एक const StructType& reference लेते हैं या caller का struct in place modify करने के लिए एक plain reference।
यहाँ सही passing style चुनना C++ में कोई भी sizeable object pass करने के लिए apply होने वाला same performance consideration है।
उदाहरण: Passing Structs to Functions
#include <iostream>
struct Point { int x; int y; };
void printPoint(const Point &p) { // avoids copying every member
std::cout << p.x << "," << p.y << std::endl;
}
int main() {
Point p = {3, 4};
printPoint(p);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक struct के closing brace के बाद semicolon भूल जाना।
- दो structs को
==से compare करना, जो default से defined नहीं है। - यह भूल जाना कि struct members default से public हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: