C++ Structures Lecture Notes (PDF)

Summary

This document is a lecture on C++ structures. It explains the concept of structures, their uses and provides examples of how they are used in C++. The lecture covers data organization techniques using structures in C++.

Full Transcript

Structured Programming Presented by: Dr. Mona Hussein Alnaggar 2024-2025 1st term Lecture 10 - Structures 1 Objectives What are Structures? Why use Structures in C++? Syntax and Declaration Accessing and Using Stru...

Structured Programming Presented by: Dr. Mona Hussein Alnaggar 2024-2025 1st term Lecture 10 - Structures 1 Objectives What are Structures? Why use Structures in C++? Syntax and Declaration Accessing and Using Structures Structures vs. Classes Practical Applications and examples What are Structures? structure in C++ is a user-defined data type. It groups different types of variables (known as members) under a single name. Key Benefit: Helps organize complex data efficiently. Example: struct Person { string name; int age; float height; }; Structures Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, string, bool, etc.). Why Use Structures in C++? 1.Data Organization: Combine related data types into one entity. 2.Code Clarity: Simplifies complex data manipulation. 3.Reuse: A single structure can be reused in various parts of the program. 4.Flexibility: Can include arrays, pointers, and even other structures. Create a Structure To create a structure, use the struct keyword and declare each of its members inside curly braces. After the declaration, specify the name of the structure variable (myStructure in the example below): struct { // Structure declaration int myNum; // Member (int variable) string myString; // Member (string variable) } myStructure; // Structure variable Syntax and Declaration Key Points: The struct keyword defines the structure. Members can be of different data types. Structures are defined outside the main function. Accessing and Using Structures Classes Structures Vs. Classes Feature Structure Class Members are public Members are private Access Specifier by default by default Limited in older C++ Fully supports Inheritance versions inheritance Complex data and Use Case Simple data grouping behavior Practical Applications of structures Database Records: Use structures to store records, e.g., student details or product catalogs. Graphics Programming: Store coordinates, colors, and shapes. File Handling: Group related file data for reading/writing. Network Protocols: Define message formats with structured data. Example: Student Management Example: Employee Record A structure to store employee details such as name, ID, position, and salary. Example3: Geometry - Points and Rectangles A structure to represent a point and a rectangle using two points. Nested Structures Examples Nested Structures Using a structure inside another structure. Student Grades A structure to store details about a student and their grades in three subjects. Address Book A structure to manage contact details, including nested address information. Multi struct variables definition: Define struct variables: struct coord { int x,y ; } first, second; Another Approach: struct coord { int x,y ; };............... struct coord first, second; struct coord third; One Structure in Multiple Variables You can use a comma (,) to use one structure in many variables: struct { int myNum; string myString; } myStruct1, myStruct2, myStruct3; // Multiple structure variables separated with commas This example shows how to use a structure in two different variables: struct { string brand; string model; int year; } myCar1, myCar2; // We can add variables by separating them with a comma here // Put data into the first structure myCar1.brand = "BMW"; myCar1.model = "X5"; myCar1.year = 1999; // Put data into the second structure myCar2.brand = "Ford"; myCar2.model = "Mustang"; myCar2.year = 1969; // Print the structure members cout

Use Quizgecko on...
Browser
Browser