Structures and Records in C++
A record groups related named fields, potentially of different types, into one composite value.
Summary
A record groups related named fields, potentially of different types, into one composite value. In C++, struct defines a user-defined type that can contain data members and member functions. Individual objects hold their own member values.
A definition such as struct FileInfo { int id; bool open; }; requires a semicolon after the closing brace. Structures and classes have the same capabilities, but their default access differs.
| Type keyword | Default member access | Default base-class access |
|---|---|---|
| struct | Public | Public |
| class | Private | Private |
For a simple aggregate, FileInfo file{7, false}; initializes members in declaration order. Access members through an object using file.open, or through a pointer using ptr->open.
Implicitly generated copy operations copy members individually. FileInfo other = file; creates a distinct object with copied member values. If a member is a raw pointer, copying it copies the address rather than duplicating the pointed-to object, so ownership requires careful design.
Records make function interfaces express complete data items and support arrays or containers of structured data, such as file metadata and compiler tokens. Structure padding may add bytes between or after members to satisfy alignment requirements.
Common Misconceptions: A struct can contain member functions, not just data. Copying raw pointer members does not duplicate their pointed-to objects. A structure's storage can include padding beyond its members.
🧠 Key Concepts
- Records
- Structure Definitions
- Default Access
- Aggregate Initialization
- Member Access
- Memberwise Copying
- Raw Pointer Ownership
- Structure Padding
🧠 Quick Check
See what you remember from the summary.
What does a record allow a program to represent?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Structures and Records in C++
📘 Overview A record groups related named fields, potentially of different types, into one composite value. C++ represents record-like data with struct, which defines a user-defined type and can also contain member functions.
🧠 Key Idea A struct defines the layout and behavior of a type; individual objects hold their own member values. Members are public by default and are accessed through an object with . or through a pointer with ->.
⚔️ Core Details: - A structure definition introduces a type: struct FileInfo { int id; bool open; }; The semicolon after the closing brace is required. - Members and base classes are public by default in a struct, but private by default in a class. - A simple aggregate supports ordered brace initialization: FileInfo file{7, false}; initializes members in declaration order. - The dot operator accesses a member through an object: file.open = true; - The arrow operator accesses a member through a pointer: FileInfo* ptr = &file; ptr->open = true; - Implicitly generated copy operations copy members individually: FileInfo other = file; creates a distinct object with copied member values.
🎯 Why It Matters: - Grouping related fields reduces mismatches between separate variables and makes function interfaces express complete data items. - Record types support arrays and containers of structured data, such as file metadata or compiler tokens. - Copying a structure with raw pointer members copies pointer addresses rather than duplicating the pointed-to objects, making ownership design essential.
🧠 Quick Recall: - Record - A composite data item containing named fields that may have different types. - struct versus class - C++ types with the same capabilities but different default member and base-class access. - Member access - object.member uses an object; pointer->member uses a pointer to an object. - Aggregate initialization - Brace initialization of an aggregate's elements, ordinarily following member declaration order. - Structure padding - Extra bytes may appear between or after members to satisfy alignment requirements.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.