Difference Between Structure and Class in C++
In C++ the ONLY real difference: struct members are public by default, class members are private by default. Everything else is identical — proof with code.
The surprising truth
struct Point {
int x; // public by default - accessible directly
};
class Point2 {
int x; // private by default - NOT accessible directly
};
Point p; p.x = 5; // OK
Point2 q; q.x = 5; // ERROR: 'x' is private
Proof: a struct doing everything a class does
#include <iostream>
using namespace std;
struct Student { // struct, not class!
string name;
int marks;
Student(string n, int m) { // constructor in a struct - valid!
name = n; marks = m;
}
void display() { // member function - valid!
cout << name << " scored " << marks << endl;
}
};
int main() {
Student s("Aman", 92);
s.display();
return 0;
}
Compiles and runs perfectly. If someone says "struct cannot have functions" — that is true for C, false for C++. Knowing this distinction clearly is exactly what interviewers check.
Second (smaller) difference: default inheritance
struct B1 : A { }; // means : PUBLIC A (struct defaults public)
class B2 : A { }; // means : PRIVATE A (class defaults private)
Same rule, applied to inheritance: struct inherits publicly by default, class privately. In practice everyone writes public explicitly, so this rarely bites — but it completes the answer.
C struct vs C++ struct (where the confusion comes from)
| Feature | C struct | C++ struct |
|---|---|---|
| Member functions | ❌ No | ✅ Yes |
| Constructors/destructors | ❌ No | ✅ Yes |
| Access specifiers | ❌ No (all public) | ✅ private/protected/public |
| Inheritance | ❌ No | ✅ Yes |
| Declaring a variable | struct Point p; | Point p; (struct word optional) |
When do C++ programmers actually use struct?
- struct — for plain data bundles with no rules to protect: a Point{x, y}, an RGB colour, a pair of values. "Everything open, come and use it."
- class — when data has rules and must be protected behind functions: BankAccount, Student with validation. "Data locked, use the official doors."
- Interview line: "Technically only default access differs; by convention, struct for passive data, class for objects with behaviour and invariants."
चौंकाने वाला सच
struct Point {
int x; // default में public - directly accessible
};
class Point2 {
int x; // default में private - directly accessible NAHI
};
Point p; p.x = 5; // OK
Point2 q; q.x = 5; // ERROR: 'x' is private
Proof: struct वह सब करते हुए जो class करती है
#include <iostream>
using namespace std;
struct Student { // struct, class नहीं!
string name;
int marks;
Student(string n, int m) { // struct में constructor - valid!
name = n; marks = m;
}
void display() { // member function - valid!
cout << name << " scored " << marks << endl;
}
};
int main() {
Student s("Aman", 92);
s.display();
return 0;
}
Perfectly compile और run होता है. कोई कहे "struct में functions नहीं हो सकते" — यह C के लिए सच है, C++ के लिए गलत. यही distinction साफ-साफ जानना interviewers check करते हैं.
दूसरा (छोटा) अंतर: default inheritance
struct B1 : A { }; // मतलब : PUBLIC A (struct का default public)
class B2 : A { }; // मतलब : PRIVATE A (class का default private)
वही rule, inheritance पर लागू: struct default में publicly inherit करता है, class privately. Practice में सब public explicitly लिखते हैं, तो यह कम ही परेशान करता है — लेकिन answer इससे पूरा होता है.
C struct vs C++ struct (confusion यहीं से आती है)
| Feature | C struct | C++ struct |
|---|---|---|
| Member functions | ❌ नहीं | ✅ हां |
| Constructors/destructors | ❌ नहीं | ✅ हां |
| Access specifiers | ❌ नहीं (सब public) | ✅ private/protected/public |
| Inheritance | ❌ नहीं | ✅ हां |
| Variable declare करना | struct Point p; | Point p; (struct शब्द optional) |
C++ programmers struct असल में कब use करते हैं?
- struct — बिना किसी rule वाले plain data bundles के लिए: Point{x, y}, RGB colour, values का pair. "सब खुला है, आओ use करो."
- class — जब data के rules हों और उसे functions के पीछे protect करना हो: BankAccount, validation वाला Student. "Data locked है, official दरवाज़ों से आओ."
- Interview line: "Technically सिर्फ default access अलग है; convention से passive data के लिए struct, behaviour और rules वाले objects के लिए class."
Frequently Asked Questions
What is the only real difference between struct and class in C++?
Default access: struct members and inheritance are public by default, class members and inheritance are private by default. Both support functions, constructors and inheritance equally.
Can a struct have constructors and member functions in C++?
Yes. A C++ struct supports constructors, destructors, member functions, access specifiers and inheritance — only the C struct lacks these.
When should I use struct instead of class?
Use struct for simple passive data bundles where everything can be open (like a Point or RGB value); use class when data has rules and needs protection behind member functions.