Skip to content
C++ Better Explained
Go back
C++ Struct vs Class: What's the Difference?
Edit page

C++ Struct vs Class: What’s the Difference?

If you’re coming from C, you know struct as a simple data container. In C++, the distinction between struct and class is much smaller than most beginners expect.

The technical difference is exactly one thing.


The One Real Difference: Default Access

struct MyStruct {
    int x;   // public by default
    int y;
};

class MyClass {
    int x;   // private by default
    int y;
};

That’s it. The only difference is what access level members get when you don’t specify one.

Everything else — constructors, methods, inheritance, templates, friend declarations — works identically in both.


Both Can Have Everything a Class Has

A struct with methods, a constructor, and encapsulation:

struct Point {
    double x, y;

    Point(double x, double y) : x(x), y(y) {}

    double distance(const Point& other) const {
        double dx = x - other.x;
        double dy = y - other.y;
        return sqrt(dx*dx + dy*dy);
    }

    void print() const {
        cout << "(" << x << ", " << y << ")" << endl;
    }
};

Point a(0, 0);
Point b(3, 4);
cout << a.distance(b) << endl;  // 5

This is perfectly valid C++. The struct has a constructor, methods, and behaves exactly like a class.


Inheritance Default Also Differs

The same rule applies to inheritance: struct uses public inheritance by default, class uses private inheritance by default.

struct Base { int x = 10; };

struct DerivedStruct : Base {};   // public inheritance (default for struct)
class DerivedClass : Base {};     // private inheritance (default for class)

DerivedStruct s;
cout << s.x;   // Fine — x is accessible (public inheritance)

DerivedClass c;
// cout << c.x;  // Error — x is inaccessible (private inheritance)

In practice, you almost always want public inheritance, so you’ll write class Derived : public Base explicitly anyway.


The Convention: When to Use Each

Since the compiler doesn’t enforce it, the C++ community follows a convention:

Use struct for:

struct Color { int r, g, b; };         // Just data
struct Point { double x, y; };         // Just data
struct Config { bool verbose; int timeout; string logFile; };  // Settings bundle

Use class for:

class BankAccount {
    double balance;  // Private — must never go negative
public:
    void deposit(double amount);
    bool withdraw(double amount);
    double getBalance() const;
};

This is a convention the compiler doesn’t enforce — you can use either keyword for either purpose. But following the convention makes your intent clear to other programmers.


Side-by-Side Comparison

// As a struct (members public by default)
struct Rectangle {
    double width, height;

    double area() const { return width * height; }
};

Rectangle r;
r.width = 5;   // Fine — public
r.height = 3;
cout << r.area();  // 15
// As a class (members private by default)
class Rectangle {
    double width, height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}
    double area() const { return width * height; }
};

Rectangle r(5, 3);
// r.width = 5;  // Error — private
cout << r.area();  // 15

The class version protects width and height from external modification. The struct version exposes them freely.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Summary Table

Featurestructclass
Default member accesspublicprivate
Default inheritancepublicprivate
Can have constructors?YesYes
Can have methods?YesYes
Can have private members?Yes (explicit)Yes (default)
Can inherit?YesYes
Templates?YesYes

Common Misconception

Many beginners think C++ structs are “limited” like C structs — just plain data, no methods. That’s not true. A C++ struct is a full-featured type identical to a class except for the default access level.

When to make the choice: ask yourself “should the data be public by default, or private by default?” If public (simple data bundle), use struct. If private (encapsulated object), use class.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ static Keyword: What It Does in Each Context
Next Post
C++ string vs char Array: Which Should You Use?