← Back to C++ Course | Chapter 8: OOP Core | Lesson 13 of 14

C++ के Structures

एक struct कई related values को एक नाम के तहत group करता है, name, age और grade के लिए अलग blanks वाले एक form की तरह। यह information को bundle करने का एक simple तरीका है।
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
// 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;
}

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++

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक struct के closing brace के बाद semicolon भूल जाना।
  2. दो structs को == से compare करना, जो default से defined नहीं है।
  3. यह भूल जाना कि struct members default से public हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.