Comprehensive Study Notes on C++ Structs and Data Management
Comparison of Data Types: Array vs. Struct
Arrays are used for variables containing data that share the same data type. An example provided is , which stores 10 integer values.
Structs (Structures) are used for variables that encompass different data types within a single entity. Examples include grouping and together.
Memory Allocation and Declaration:
- For standard types like , , and , as well as arrays, memory is allocated in the computer during declaration.
- Example: allocates a single integer space.
- Example: allocates two adjacent spaces labeled and .
Struct Definition and Declaration Protocols
To allocate memory for a struct, both a definition and a declaration are required.
Struct Definition Syntax:
- A struct is defined with the keyword followed by a name (e.g., ).
- The structural members are placed inside curly braces.
- Example:
Defining the Structure:
- Members include:
Struct Declaration:
- Once defined, you can declare instances of that struct.
- Example: and
Combined Definition and Declaration Syntax:
- A struct can be defined and declared simultaneously by placing variable names after the closing brace but before the semicolon.
- Example:
Initialization and Member Access
Initialization of a Struct:
- Data can be assigned to a struct using curly braces in chronological order of the definition.
- Example:
Member Access Operators:
- Arrays use indexes or subscripts (e.g., ).
- Structs use the Dot Operator or Member Access Operator ().
- Example: refers to the name member of the instance.
Core Operations on Structs
Assignment (Input Data):
- Values can be assigned directly to specific members.
- Example:
Standard Input ():
- Members can be populated via user input.
- Example:
Standard Output ():
- Members are displayed using the dot operator.
- Example:
Dynamic Variables and Memory Management
Dynamic variables are constructed during the execution of a program ().
Keyword:
- Used to build a new temporary variable by allocating a memory location indicated by a pointer.
Keyword:
- Used to destroy a previously built dynamic variable by removing that specific memory location.
Practical Exercises and Scenario-Based Coding
Question 1: Car Information Management
- Structure Definition:
- Task (i): Declare a single instance named .
- Solution:
- Task (ii): Declare an array named for 20 cars.
- Solution:
- Task (iii): Write input statements for both.
- For : Uses standard flow for all four members.
- For : Uses a loop where ranges from to , accessing .
Question 2: Camera Database Application
- Structure Definition: Includes , , , , and .
- Task (a): Declare an array for 25 cameras.
- Solution:
- Task (b): Segment for inputting data.
- Uses a loop with variable from to to input data for each cataloged camera.
- Task (c): Filter display based on price.
- Logic:
Question 3: Info_Kad Debit Card Records
- Structure Definition: , , .
- Task (a): Declare for 10 customers.
- Solution:
- Task (b): Main function for input/output.
- Utilizes two separate loops (typically using index ) to carry out sequential input and output for all 10 records.
Question 4: Staff Personal Records
- Structure Definition ():
- (Last Name)
- (First Name)
- (Age)
- Task (a): Declaration for 10 staffs.
- Solution:
- Task (b): Implement main function to manage academic staff.
- Solution includes prompts: "Masukkan nama Pertama", "Masukkan nama Terakhir", and "Masukkan umur".
- The output combines both names and the age in one line: