Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C++ · 04 Jul 2026 · Hindi + English

C++ में Structure और Class में अंतर

C++ में इकलौता असली अंतर: struct के members default में public, class के default में private. बाकी सब identical — code proof के साथ.

The surprising truth

In C++, struct and class are almost identical twins. Both can have member functions, constructors, destructors, inheritance — everything. The ONLY real difference: struct members are public by default, class members are private by default. That's it. Everything else you may have heard is about C's struct, not C++'s.
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;
}
Aman scored 92

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)

FeatureC structC++ struct
Member functions❌ No✅ Yes
Constructors/destructors❌ No✅ Yes
Access specifiers❌ No (all public)✅ private/protected/public
Inheritance❌ No✅ Yes
Declaring a variablestruct 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."

चौंकाने वाला सच

C++ में struct और class लगभग जुड़वां हैं. दोनों में member functions, constructors, destructors, inheritance — सब कुछ हो सकता है. इकलौता असली अंतर: struct के members default में public होते हैं, class के default में private. बस. बाकी जो कुछ सुना है वह C के struct की बात है, C++ के struct की नहीं.
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;
}
Aman scored 92

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 यहीं से आती है)

FeatureC structC++ 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

C++ में struct और class में इकलौता असली अंतर क्या है?

Default access: struct के members और inheritance default में public हैं, class के default में private. Functions, constructors और inheritance दोनों में बराबर supported हैं.

क्या C++ में struct में constructors और member functions हो सकते हैं?

हां. C++ struct constructors, destructors, member functions, access specifiers और inheritance support करता है — सिर्फ C के struct में ये नहीं होते.

Class की जगह struct कब use करें?

Simple passive data bundles के लिए struct use करें जहां सब खुला रह सकता है (जैसे Point या RGB value); class तब जब data के rules हों और उसे member functions के पीछे protection चाहिए.