logo

מבני C++

ב-C++, מחלקות ומבנים הם שרטוטים המשמשים ליצירת מופע של מחלקה. מבנים משמשים לאובייקטים קלים כמו מלבן, צבע, נקודה וכו'.

בניגוד למחלקה, מבנים ב-C++ הם סוג ערך מאשר סוג הפניה. זה שימושי אם יש לך נתונים שלא מיועדים לשינוי לאחר יצירת המבנה.

מדינות בארה"ב

מבנה C++ הוא אוסף של סוגי נתונים שונים. זה דומה למחלקה שמחזיקה סוגים שונים של נתונים.

התחביר של מבנה

 struct structure_name { // member declarations. } 

בהכרזה לעיל, מבנה מוכרז בקדמת ה- מילת מפתח struct ואחריו המזהה (שם המבנה). בתוך הפלטה המתולתלת, אנו יכולים להכריז על משתני האיברים מסוגים שונים. שקול את המצב הבא:

 struct Student { char name[20]; int id; int age; } 

במקרה שלעיל, Student הוא מבנה המכיל שלושה משתנים שם, מזהה וגיל. כאשר המבנה מוכרז, לא מוקצה זיכרון. כאשר המשתנה של מבנה נוצר, אז הזיכרון מוקצה. בואו נבין את התרחיש הזה.

כיצד ליצור את המופע של Structure?

ניתן להגדיר משתנה מבנה כך:

סטודנטים;

עולם הוומפוס

כאן, s הוא משתנה מבנה מסוג סטוּדֶנט . כאשר משתנה המבנה נוצר, הזיכרון יוקצה. מבנה התלמיד מכיל משתנה char אחד ושני משתנים שלמים. לכן, הזיכרון עבור משתנה char אחד הוא 1 בייט ושני ints יהיו 2*4 = 8. סך הזיכרון שנכבש על ידי המשתנה s הוא 9 בתים.

כיצד לגשת למשתנה של Structure:

ניתן לגשת למשתנה של המבנה על ידי שימוש פשוט במופע של המבנה ואחריו אופרטור הנקודה (.) ולאחר מכן בשדה של המבנה.

מי המציא את בית הספר

לדוגמה:

 s.id = 4; 

בהצהרה לעיל, אנו ניגשים לשדה המזהה של המבנה Student באמצעות ה- נְקוּדָה(.) אופרטור ומקצה את הערך 4 לשדה המזהה.

דוגמה למבנה C++

בוא נראה דוגמה פשוטה של ​​struct Rectangle שיש לו שני איברי נתונים רוחב וגובה.

 #include using namespace std; struct Rectangle { int width, height; }; int main(void) { struct Rectangle rec; rec.width=8; rec.height=5; cout&lt;<'area of rectangle is: '<<(rec.width * rec.height)<<endl; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Area of Rectangle is: 40 </pre> <h2>C++ Struct Example: Using Constructor and Method</h2> <p>Let&apos;s see another example of struct where we are using the constructor to initialize data and method to calculate the area of rectangle.</p> <pre> #include using namespace std; struct Rectangle { int width, height; Rectangle(int w, int h) { width = w; height = h; } void areaOfRectangle() { cout&lt;<'area of rectangle is: '<<(width*height); } }; int main(void) { struct rec="Rectangle(4,6);" rec.areaofrectangle(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Area of Rectangle is: 24 </pre> <p> <strong>Structure v/s Class</strong> </p> <table class="table"> <tr> <th>Structure</th> <th>Class</th> </tr> <tr> <td>If access specifier is not declared explicitly, then by default access specifier will be public. </td> <td>If access specifier is not declared explicitly, then by default access specifier will be private.</td> </tr> <tr> <td>Syntax of Structure: <br> <br> struct structure_name <br> { <br> // body of the structure. <br> }</td> <td>Syntax of Class: <br> <br> class class_name <br> { <br> // body of the class. <br> }</td> </tr> <tr> <td>The instance of the structure is known as &apos;Structure variable&apos;. </td> <td>The instance of the class is known as &apos;Object of the class&apos;.</td> </tr> </table></'area></pre></'area>

דוגמה למבנה C++: שימוש בקונסטרוקטור ושיטה

בואו נראה דוגמה נוספת למבנה שבו אנו משתמשים בבנאי כדי לאתחל נתונים ושיטה לחישוב שטח המלבן.

 #include using namespace std; struct Rectangle { int width, height; Rectangle(int w, int h) { width = w; height = h; } void areaOfRectangle() { cout&lt;<\'area of rectangle is: \'<<(width*height); } }; int main(void) { struct rec="Rectangle(4,6);" rec.areaofrectangle(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Area of Rectangle is: 24 </pre> <p> <strong>Structure v/s Class</strong> </p> <table class="table"> <tr> <th>Structure</th> <th>Class</th> </tr> <tr> <td>If access specifier is not declared explicitly, then by default access specifier will be public. </td> <td>If access specifier is not declared explicitly, then by default access specifier will be private.</td> </tr> <tr> <td>Syntax of Structure: <br> <br> struct structure_name <br> { <br> // body of the structure. <br> }</td> <td>Syntax of Class: <br> <br> class class_name <br> { <br> // body of the class. <br> }</td> </tr> <tr> <td>The instance of the structure is known as &apos;Structure variable&apos;. </td> <td>The instance of the class is known as &apos;Object of the class&apos;.</td> </tr> </table></\'area>

מבנה v/s Class

מִבְנֶה מעמד
אם מפרט הגישה לא הוכרז במפורש, אז כברירת מחדל, מפרט הגישה יהיה ציבורי. אם מפרט הגישה לא הוכרז במפורש, אז כברירת מחדל, מפרט הגישה יהיה פרטי.
תחביר של מבנה:

struct structure_name
{
// גוף המבנה.
}
תחביר של מחלקה:

class_name
{
// גוף הכיתה.
}
המופע של המבנה ידוע בשם 'משתנה מבנה'. המופע של המחלקה ידוע בשם 'אובייקט המחלקה'.