In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.

However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)

While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.

Answer from Antal Spector-Zabusky on Stack Overflow
Top answer
1 of 7
181

In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.

However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)

While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.

2 of 7
17

Other that the differences in the default access (public/private), there is no difference.

However, some shops that code in C and C++ will use "class/struct" to indicate that which can be used in C and C++ (struct) and which are C++ only (class). In other words, in this style all structs must work with C and C++. This is kind of why there was a difference in the first place long ago, back when C++ was still known as "C with Classes."

Note that C unions work with C++, but not the other way around. For example

union WorksWithCppOnly{
    WorksWithCppOnly():a(0){}
    friend class FloatAccessor;
    int a;
private:
    float b;
};

And likewise

typedef union friend{
    int a;
    float b;
} class;

only works in C

🌐
Reddit
reddit.com › r/cplusplus › struct vs class: when do you use which one and why ?
r/Cplusplus on Reddit: struct vs class: when do you use which one and why ?
September 19, 2025 -

Hi !

I'm coming from a Java background and am used to create classes.
But in C++ you have also structures.
When would you use a struct and when a class ?

Practical example:

For learning purposes, I'm creating a program which plots geographical locations on a window.
My "Java instinct" tells me to create a CPoint class containing:
string id;
double lat;
double lng;
int x;
int y;
and a protected method "translate" to do the conversion.
a constructor to call translate() whenever a new object is created, generating x and y

in C++: Could I do that also using a struct and why ? or why not ?

Thanks a lot Redditers ! :-)

🌐
Cplusplus
cplusplus.com › forum › beginner › 28083
Structs vs Classes - C++ Forum
Struct already exist in C so when C++ was created and to make sure legacy C code still run perfect using a C++ compiler, the struct is retained. Classes was introduced in C++ with protection specifiers so I believe to make C struct co-exist peacefully with C++, they default struct protection ...
🌐
Quora
quora.com › How-are-structs-in-C-different-from-classes-in-C++
How are structs in C different from classes in C++? - Quora
Answer (1 of 5): Some of the answers here miss the most important point, I think. The question is complicated by some subtleties. But let’s look at the big picture… A struct in C is a simple data record. It can combine any number of data fields, called members.
🌐
TutorialsPoint
tutorialspoint.com › article › c-cplusplus-struct-vs-class
C/C++ Struct vs Class
March 15, 2026 - In C, structures provide a way ... behavior. While C lacks the class concept, structures serve as the foundation for data organization and can simulate object-oriented features when combined with function pointers....
🌐
Mendix
blogs.sw.siemens.com › embedded-software › 2014 › 06 › 02 › struct-vs-class-in-c
struct vs class in C++ - Embedded Software
March 26, 2026 - In a struct they are public; in a class they are private. Having imparted this information, I urge you not to exploit it too heavily. A key priority when you are writing code is to ensure that it is readable [=maintainable]. Somebody – it ...
🌐
Quora
quora.com › In-C-is-a-struct-pretty-much-a-class
In C, is a struct pretty much a class? - Quora
Answer (1 of 6): No, for two reasons. First, C is not an object-oriented language. That doesn’t mean that you can’t do object-oriented programming in it. What it does mean is that the language itself provides no support for object-oriented concepts like classes, instances, etc.
Find elsewhere
🌐
Medium
medium.com › @seanoughton › c-structs-vs-classes-friends-d58461c8a028
C++ — Structs vs. Classes & Friends | by Sean Oughton | Medium
July 7, 2021 - A Struct is a container for data, similar to a record in other programming languages. It is essentially the same as a Class …
🌐
Quora
quora.com › What-is-the-difference-between-class-and-struct-in-the-C-C-programming-language-Why-would-one-use-struct-instead-of-class-if-both-have-similar-features-e-g-member-variables
What is the difference between 'class' and 'struct' in the C/C++ programming language? Why would one use struct instead of class if both have similar features (e.g., member variables)? - Quora
Answer (1 of 4): C and C++ are two separate programming languages, evolving along separate paths. They are not the same language. They are not one language. And neither is or has ever been a proper subset or superset of the other. With that out of the way, C has struct but C does not have class....
🌐
Educative
educative.io › answers › when-to-use-a-class-and-a-struct-in-cpp
When to use a class and a struct in C++?
Classes are often used for encapsulation, where data and the operations on that data are tightly bound together, and access to the data is controlled through public interfaces (public member functions). While structs are commonly used for data structures where the emphasis is on holding data rather than providing encapsulated behavior.
🌐
Medium
medium.com › @nonuruzun › struct-vs-class-in-c-and-c-2adf28d81c4d
Struct vs Class in C++ and C. In C++, structs and classes are pretty… | by Onur Uzun | Medium
November 9, 2017 - In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › structure-vs-class-in-cpp
Difference Between Structure and Class in C++ - GeeksforGeeks
In C++, both structures (struct) and classes (class) are user-defined data types used to group related data and functions into a single unit. They support almost the same features, including member functions, constructors, inheritance, and polymorphism.
Published: July 3, 2026
🌐
Reddit
reddit.com › r/learnprogramming › when would a data structure be used in a class vs a struct?
r/learnprogramming on Reddit: When would a data structure be used in a class vs a struct?
November 22, 2023 -

Edit 2: The language is C++. sorry everyone

So I have been going through my course instruction using classes to implement data structures, yet LeetCode uses structs to implement them. I read that structs are faster than classes in that they do not use heap memory.

Edit: Someone has pointed out that both can be allocated on the heap and the stack

What is the purpose of implementing a data structure via a class if structs can be used? Sorry if this is a dumb question

🌐
Cplusplus
cplusplus.com › forum › beginner › 21876
struct vs class - C++ Forum
I personally use a struct when I know all members would be public. But you can use a class always anyway.
Top answer
1 of 16
1143

The differences between a class and a struct in C++ are:

  • struct members and base classes/structs are public by default.
  • class members and base classes/structs are private by default.

Both classes and structs can have a mixture of public, protected and private members, can use inheritance, and can have member functions.

I would recommend you:

  • use struct for plain-old-data structures without any class-like features;
  • use class when you make use of features such as private or protected members, non-default constructors and operators, etc.
2 of 16
302

As everyone else notes there are really only two actual language differences:

  • struct defaults to public access and class defaults to private access.
  • When inheriting, struct defaults to public inheritance and class defaults to private inheritance. (Ironically, as with so many things in C++, the default is backwards: public inheritance is by far the more common choice, but people rarely declare structs just to save on typing the "public" keyword.

But the real difference in practice is between a class/struct that declares a constructor/destructor and one that doesn't. There are certain guarantees to a "plain-old-data" POD type, that no longer apply once you take over the class's construction. To keep this distinction clear, many people deliberately only use structs for POD types, and, if they are going to add any methods at all, use classes. The difference between the two fragments below is otherwise meaningless:

class X
{
  public:

  // ...
};

struct X
{
  // ...
};

(Incidentally, here's a thread with some good explanations about what "POD type" actually means: What are POD types in C++?)

🌐
LinkedIn
linkedin.com › pulse › structures-c-vs-classes-santosh-s-malagi
Structures (C vs C++) and Classes (C++ vs C++)
April 24, 2021 - A struct is used when it makes sense to logically group several related elements together in a certain context. Contrary to a struct, a class is made to offer an interface, that has some degree of separation from its implementation.
🌐
Medium
medium.com › @imani.sajad92 › struct-vs-class-b4c89ba88070
Struct vs Class. Advantages and Disadvantages | by Sajad Imani | Medium
February 10, 2025 - Struct is value type and it is copied by value, meaning that a complete replica of the data is created when passed or assigned. Class is reference type and it is copied by reference, meaning that only a reference to the original data is passed ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 136888
Struct and Class - C++ Forum
Structs are used to group data together(good for passive data), that data is public by default, and a struct cannot use functions. Classes are more powerful, they also group data, their data members are private by default, and they can use member functions. The data in a class has a strong ...
🌐
Quora
quora.com › What-is-the-difference-between-struct-and-class-keywords-in-C-C
What is the difference between 'struct' and 'class' keywords in C/C++? - Quora
Answer (1 of 7): Ah, that somewhat mythical language C/C++. C and C++ are two different languages. See? ^\textit{Yeah, yeah, I know. I needed an image for this answer's “preview” view.}\tag*{} C/C++, as best as I can tell, is C code that’s been mechanically converted to C++ in a manner ...