struct vs class
The one real difference between C++'s struct and class -- and how to choose between them.
What you'll learn
- State the one default-access difference between struct and class in C++
- Explain that C++'s struct, unlike C's, can have constructors and methods
- Choose struct for simple data bundles and class for types with invariants to protect
Explanation
Unlike C's struct (a plain data bundle with no methods at all), a C++ struct is, technically, exactly as capable as a class -- it can have constructors, destructors, methods, and even inheritance. The only actual difference between them in C++ is their default access level: a struct's members are public by default, while a class's members are private by default. Everything else -- what you can do with either one -- is identical.
Because that's the only technical difference, the choice between struct and class in real C++ code is really a matter of convention and intent, not capability. struct is idiomatically used for simple, passive data bundles with no real invariants to protect -- a Point with public x/y fields that's fine for any code to read or write freely. class is idiomatically used when a type has behavior and rules to enforce -- private data, validated through a deliberate public interface, as you saw with BankAccount in the previous lesson.
This convention matters for readability: seeing struct Point signals "plain data, feel free to touch the fields directly," while seeing class Vector2D signals "there's a real interface here, don't assume you can reach into its internals." Following the convention, even though the compiler would technically let you write either one either way, makes your code's intent legible to anyone else reading it.
Guided lab
Predict: A plain struct alongside a class with methods
Read this program and predict exactly what it prints.
#include <iostream>
struct Point {
int x;
int y;
};
class Vector2D {
public:
Vector2D(int x, int y) : x_(x), y_(y) {}
int lengthSquared() const {
return x_ * x_ + y_ * y_;
}
private:
int x_;
int y_;
};
int main() {
Point p{3, 4};
std::cout << "Point: (" << p.x << ", " << p.y << ")" << std::endl;
Vector2D v(3, 4);
std::cout << "Length squared: " << v.lengthSquared() << std::endl;
return 0;
}Stuck? Get a hint.
Common mistakes
- Assuming C++'s struct is limited to plain data like C's struct is -- in C++, struct can have constructors, methods, and everything class can.
- Forgetting the one real difference between struct and class: default member access is public for struct, private for class.
- Using struct for a type with real invariants to protect (which signals 'plain data, touch freely' to readers) instead of using class for that case.
Knowledge check
Takeaway
struct and class differ only in default member access (public vs private) -- choosing between them is a readability convention, not a capability difference.
Summary
C++'s struct is as capable as class, differing only in default access (public vs private); convention uses struct for plain data and class for types with an enforced interface.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.