Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Sunday, August 30, 2026


Structure vs Union vs Class in C++

BasisStructureUnionClass
Keywordstructunionclass
PurposeGroups related data and functionsSaves memory by sharing memory among membersImplements objects and OOP concepts
Default Access Specifierpublicpublicprivate
Memory AllocationSeparate memory for each data memberSame memory is shared by all data membersSeparate memory for non-static data members
Values of MembersAll members can hold values simultaneouslyNormally, only one member is the active member at a timeAll members can hold values simultaneously
Member FunctionsYesYesYes
EncapsulationSupportedLimitedStrongly supported
InheritanceSupportedNot supportedSupported
PolymorphismSupportedNot supported through inheritanceSupported
Data HidingPossible using privatePossible using access specifiersAvailable and commonly used
Object CreationStudent s;Data d;Student s;
Memory RequirementGenerally greater than a unionGenerally equal to the size needed for the largest member, considering alignmentDepends on data members and alignment/padding
Main AdvantageSimple grouping of related dataEfficient memory utilizationData abstraction and OOP
Main LimitationPublic by defaultMembers cannot ordinarily be used simultaneously as independent stored valuesPrivate by default may require access functions
Typical ExampleStudent recordStore either int, float, or charStudent/Employee/Bank Account object
Basic Syntaxstruct A { int x; };union A { int x; float y; };class A { int x; };

Syntax Comparison

// Structure
struct Student
{
    int rollNo;
    string name;
};

// Union
union Data
{
    int i;
    float f;
};

// Class
class Student
{
private:
    int rollNo;

public:
    void display()
    {
        cout << rollNo;
    }
};

One Program Demonstrating All Three

#include <iostream>
using namespace std;

// Structure
struct StudentStruct
{
    int rollNo;
};

// Union
union Data
{
    int i;
    float f;
};

// Class
class StudentClass
{
private:
    int rollNo;

public:
    void setRollNo(int r)
    {
        rollNo = r;
    }

    void display()
    {
        cout << "Class Roll No: " << rollNo << endl;
    }
};

int main()
{
    // Structure
    StudentStruct s;
    s.rollNo = 101;

    // Union
    Data d;
    d.i = 50;

    // Class
    StudentClass c;
    c.setRollNo(102);

    cout << "Structure Roll No: " << s.rollNo << endl;
    cout << "Union Integer: " << d.i << endl;
    c.display();

    return 0;
}

Output:

Structure Roll No: 101
Union Integer: 50
Class Roll No: 102

 Most Important Exam Points

StructureUnionClass
structunionclass
Public by defaultPublic by defaultPrivate by default
Separate memoryShared memorySeparate memory
All members can be used togetherOne active member at a timeAll members can be used together
Supports OOP featuresLimitedStrong OOP support

One-line memory trick:
Structure = Grouping, Union = Memory Sharing, Class = Encapsulation + OOP.

No comments:

Post a Comment