Programming Pandit

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


Latest Update

Sunday, August 30, 2026

Structure vs Union vs Class in C++


Structure vs Union vs Class in C++

Basis

Structure

Union

Class

Keyword

struct

union

class

Purpose

Groups related data and functions

Saves memory by sharing memory among members

Implements objects and OOP concepts

Default Access Specifier

public

public

private

Memory Allocation

Separate memory for each data member

Same memory is shared by all data members

Separate memory for non-static data members

Values of Members

All members can hold values simultaneously

Normally, only one member is the active member at a time

All members can hold values simultaneously

Member Functions

Yes

Yes

Yes

Encapsulation

Supported

Limited

Strongly supported

Inheritance

Supported

Not supported

Supported

Polymorphism

Supported

Not supported through inheritance

Supported

Data Hiding

Possible using private

Possible using access specifiers

Available and commonly used

Object Creation

Student s;

Data d;

Student s;

Memory Requirement

Generally greater than a union

Generally equal to the size needed for the largest member, considering alignment

Depends on data members and alignment/padding

Main Advantage

Simple grouping of related data

Efficient memory utilization

Data abstraction and OOP

Main Limitation

Public by default

Members cannot ordinarily be used simultaneously as independent stored values

Private by default may require access functions

Typical Example

Student record

Store either int, float, or char

Student/Employee/Bank Account object

Basic Syntax

struct 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

Structure

Union

Class

struct

union

class

Public by default

Public by default

Private by default

Separate memory

Shared memory

Separate memory

All members can be used together

One active member at a time

All members can be used together

Supports OOP features

Limited

Strong OOP support

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

No comments:

Post a Comment