Topic 37 of 91 7. C++ Structures and Unions Interactive Tutorial

Structures in C++: Building Custom Data Records With struct

15 mins read Last updated: 26 Sep 2026 21,600 views

Direct Answer & Core Concept

A structure (struct) in C++ is a user-defined composite type that groups heterogeneous data members under a single identifier. In C++, the ONLY technical difference between a struct and a class is default member access: struct members and base classes are public by default, whereas class members and base classes are private by default. Both support constructors, member functions, inheritance, and templates.

Detailed Explanation & Mechanics

1. Struct vs Class Default Access:

Architecture Diagram
C++ STRUCT VS CLASS:
┌─────────────────┬──────────────────────────┬────────────────────────────┐
│ Feature         │ struct                   │ class                      │
├─────────────────┼──────────────────────────┼────────────────────────────┤
│ Default Members │ PUBLIC                   │ PRIVATE                    │
│ Default Inherit │ PUBLIC                   │ PRIVATE                    │
│ Idiomatic Use   │ Passive Data Structures  │ Encapsulated Objects / OOP │
└─────────────────┴──────────────────────────┴────────────────────────────┘

2. Struct Memory Alignment & Padding:

Compilers insert padding bytes between struct members to align them to CPU word boundaries for faster memory bus transfers (alignof(Type)).

Runnable Code Example: Structures in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>

// Plain Old Data (POD) / Aggregate struct
struct DatabaseRecord {
    int recordId;
    std::string tableName;
    double queryLatencyMs;

    void display() const {
        std::cout << "[Record #" << recordId << "] Table: " << tableName 
                  << " | Latency: " << queryLatencyMs << " ms
";
    }
};

int main() {
    // Designated initializers (C++20)
    DatabaseRecord record{1001, "users_auth", 1.42};
    record.display();

    std::cout << "DatabaseRecord Size: " << sizeof(record) << " bytes
";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is struct memory padding and alignment in C++?

A: CPU hardware reads memory efficiently in aligned chunks (e.g. 4 or 8 bytes). If a 1-byte char is followed by an 8-byte double, the compiler inserts 7 padding bytes to ensure the double starts at a multiple of 8.
Q

Q: What is an aggregate struct in C++?

A: An aggregate is a class/struct with no user-declared constructors, no private/protected non-static data members, and no virtual functions, allowing direct brace initialization `StructName{a, b, c}`.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Use struct for passive Plain Old Data (POD) / Data Transfer Objects (DTOs).
2. Use class when enforcing invariants, private encapsulation, and complex behavior.
3. Order struct data members from largest to smallest to minimize memory padding waste.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.