Skip to main content

Command Palette

Search for a command to run...

Struct vs Class

Updated
4 min read
Struct vs Class
D

I'm an Engineer

1. Historical Context: Why Both Exist

Structs — From C

  • struct originates from C

  • Used to group related data

  • No encapsulation, no methods (in C)

Classes — From C++

  • Introduced to support Object-Oriented Programming

  • Encapsulation, abstraction, inheritance, polymorphism

C++ deliberately kept struct to:

  • Maintain C compatibility

  • Support Plain Old Data (POD) and low-level programming

  • Enable expressive design choices


2. Basic Syntax Comparison

Struct

struct Point {
    int x;
    int y;
};

Class

class Point {
    int x;
    int y;
};

At this level, they look almost identical, but behavior differs due to defaults.


3. The Most Important Difference: Default Access Specifier

Struct Defaults

struct A {
    int x;        // public by default
};

Class Defaults

class B {
    int x;        // private by default
};
Featurestructclass
Default accesspublicprivate
Default inheritancepublicprivate

This single difference influences design intent, readability, and API contracts.


4. Access Specifiers Work the Same Way

Both struct and class support:

  • public

  • protected

  • private

struct S {
private:
    int a;

public:
    void set(int x) { a = x; }
};
  • Structs can be fully encapsulated

  • Classes can expose public data


5. Constructors, Destructors, and Methods

Structs can do everything classes can.

struct Sensor {
    int id;

    Sensor(int id) : id(id) {}

    ~Sensor() {
        // cleanup
    }

    void print() const {
        std::cout << id << std::endl;
    }
};

✔ Constructors
✔ Destructors
✔ Member functions
const correctness


6. Inheritance: Same Rules, Different Defaults

Struct Inheritance (Public by Default)

struct Base {
    int x;
};

struct Derived : Base {
    void print() { std::cout << x; }
};

Class Inheritance (Private by Default)

class Derived : Base {   // private inheritance
};

To match struct behavior:

class Derived : public Base {
};

7. Polymorphism Works Identically

Virtual functions behave the same.

struct Shape {
    virtual double area() const = 0;
    virtual ~Shape() = default;
};
  • Virtual table (vtable) generated

  • Runtime polymorphism enabled

  • Same cost and behavior


8. POD, Trivial, and Standard Layout Types

This is where structs are preferred, especially in systems programming.

Plain Old Data (POD)

A struct with:

  • No virtual functions

  • No private members

  • No custom constructors

struct PacketHeader {
    uint16_t type;
    uint16_t length;
};

Why this matters:

  • Can be memcpy’d

  • Compatible with C

  • Matches hardware / network layouts

  • Used in drivers, protocols, DMA buffers

Classes can be POD, but structs signal intent better.


9. Aggregate Initialization

struct Vec3 {
    float x, y, z;
};

Vec3 v = {1.0f, 2.0f, 3.0f};

Aggregate initialization:

  • Clean

  • Fast

  • Predictable

Once you add private members or constructors, this is disabled.


10. Design Intent

Use struct when:

  • Data-centric objects

  • Simple grouping of fields

  • Passive data carriers

  • C compatibility

  • Low-level memory mapping

  • Configuration objects

struct CameraConfig {
    int width;
    int height;
    float fps;
};

Use class when:

  • Invariants must be enforced

  • Behavior > data

  • Abstraction boundaries

  • Public API contracts

  • Complex lifecycle management

class Camera {
public:
    void start();
    void stop();

private:
    CameraConfig config_;
};

Struct = “What it is” and Class = “What it does”


11. Encapsulation Is a Choice, Not a Keyword

Bad design:

struct User {
    std::string password;
};

Good design:

struct User {
private:
    std::string password;

public:
    bool authenticate(std::string_view input) const;
};

Struct does not mean “no encapsulation”.


13. Performance

  • No runtime difference

  • No performance difference

  • No extra indirection

  • Same calling conventions

Choosing struct or class does not affect performance.


14. Rule of Thumb

If users should NOT care about internals → use class
If users SHOULD see and manipulate data → use struct


15. Final Summary Table

Aspectstructclass
Default accesspublicprivate
Default inheritancepublicprivate
Memory layoutSameSame
MethodsYesYes
PolymorphismYesYes
EncapsulationOptionalTypical
C compatibilityBetterWorse
Design intentDataBehavior

8 views

More from this blog

The Engineering Hub

11 posts

knowledge sharing portal...