The short answer
In C++, a struct and a class are both class types, and for most purposes they can do the same things. The main language difference is the default access level. Members of a struct are public unless you say otherwise, and members of a class are private unless you say otherwise. Inheritance follows a similar default, which is covered below.
Because the difference is so small, the choice is mostly about convention. Many programmers use struct for a simple type that just groups related data, and class for a type that hides its internal details and keeps its own rules. Being consistent within a project matters more than any single rule.
Default access
Is struct private or public by default? Public. A class defaults to private. Here is the same kind of type written both ways:
struct PointStruct {
int x; // public by default
int y;
};
class PointClass {
int x; // private by default
int y;
};
int main() {
PointStruct a;
a.x = 1; // OK: x is public
PointClass b;
// b.x = 1; // If you uncomment this line, the compiler rejects it
}
The assignment to a.x compiles. To make class members public, add a public: label above them. The full set of access labels, including protected, is covered in access modifiers in c.
Inheritance defaults
The same idea applies to base classes. With struct Derived : Base, the base is inherited publicly. With class Derived : Base, the base is inherited privately.
struct Base {
void hello() {}
};
struct PublicChild : Base {}; // Base is inherited publicly
class PrivateChild : Base {}; // Base is inherited privately
This matters when code outside the class wants to treat the derived object as a Base. A public base allows that conversion, and a private base does not. The wider rules for inheritance in c cover how base classes and overrides work.
What both keywords can do
Both struct and class can contain:
- data members and member functions
- constructors and a destructor
staticmembersvirtualfunctions, which support polymorphism- templates
frienddeclarations- overloaded operators
Because of that, the keyword does not decide what a type can do. What it decides is how much of the type is hidden by default. Hiding internal state behind a small set of functions is the core idea of encapsulation in c.
A struct can also have a constructor that sets valid starting values, just as a class can. The details are in c struct constructor.
Aggregates and brace initialization
A type that has only public data members and no user-declared constructors is an aggregate. You can set its members with braces:
struct Point {
int x;
int y;
};
int main() {
Point p{3, 4}; // aggregate initialization
}
Whether a type is an aggregate depends on its members and base classes, not on the keyword. A class with only public data members and no constructors can use the same brace syntax. A class with private data members, a user-declared constructor, or virtual functions cannot.
Which one should you use?
A common approach looks like this:
- Use
structfor a simple type that just holds data and has no rules to protect. - Use
classwhen the type has rules to keep, such as a value that must stay in a range, or internal state that outside code should not change. - Pick one convention for the project and apply it to new types.
Some widely used C++ style guides draw the line in a similar place.
Mixing the two keywords
Because both keywords declare the same kind of type, you can forward-declare a type with one and define it with the other. Some compilers warn about the mismatch, so it is usually better to use the same keyword everywhere.
Structs in C
Can you use structs in C? Yes. C has structs, but they hold data only. A C struct cannot have member functions, access labels, inheritance or constructors. Functions that work with a struct are written separately and take the struct as an argument. Here is a short example:
#include <stdio.h>
struct Rectangle {
double width;
double height;
};
double area(struct Rectangle r) {
return r.width * r.height;
}
int main(void) {
struct Rectangle box = {3.0, 4.5};
printf("%.1f\n", area(box));
return 0;
}
The program prints 13.5. In C, you write struct Rectangle each time you use the type, unless you add a typedef. Storing several structs together is covered in array of structs in c.