←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Static Data Members & Static Member Functions in C++

Study material and explanations for Unit 3

Static Data Members & Static Member Functions in C++

1. What are Static Data Members? (In Simple Words)

Normally, in a C++ class, every object gets its own separate copy of variables. If you create 10 student objects, there will be 10 separate rollNo variables in RAM.

But what if you want a single variable that is shared by ALL objects together? For example:

  • Counting the total number of students currently enrolled.
  • Storing a common bank interest rate that applies to all accounts.

A Static Data Member is a class variable marked with the static keyword. Only one single copy is created in memory, and every object shares this same copy!

Instance Variables vs. Shared Static Class Variable
Object 1 (emp1)
id = 101, salary = 40000
── Shares ──▶
ONE SHARED STATIC VARIABLE
static int totalCount = 2
(Stored only once in Global Memory)
◀── Shares ──
Object 2 (emp2)
id = 102, salary = 55000

Key Rules for Static Data Members:

  1. Single Copy: Created only once when the program starts.
  2. Auto-Zero: If you don’t give it any initial value, C++ automatically sets it to zero (0).
  3. Mandatory Outside Definition: Declaring it inside the class is not enough. You must define it outside the class using the Scope Resolution Operator (::) to allocate real memory.

2. What are Static Member Functions?

Just like static variables, you can also make member functions static.

Rules for Static Member Functions (Golden Exam Points):

  1. No Object Needed: You can call a static function directly using the class name: ClassName::functionName();. You don’t have to create any object first!
  2. Access Restrictions: A static function can ONLY access static variables or other static functions. It CANNOT touch normal non-static variables (like id or name)!
  3. No this Pointer: Because a static function doesn’t belong to any particular object, it has no this pointer.

3. Complete Working Example: Employee Counter

#include <iostream>
#include <string>
using namespace std;

class Employee {
    private:
        int id;
        string name;
        
        // 1. Declare static variable inside class
        static int totalCount; 

    public:
        void setDetails(int i, string n) {
            id = i;
            name = n;
            totalCount++; // Increment shared counter whenever an employee is added
        }

        void display() {
            cout << "ID: " << id << ", Name: " << name << endl;
        }

        // 2. Declare static function
        static void showTotalEmployees() {
            // Can access totalCount, but CANNOT access 'id' or 'name'!
            cout << "Total Active Employees: " << totalCount << endl;
        }
};

// 3. MANDATORY: Define and initialize outside the class!
int Employee::totalCount = 0; 

int main() {
    // Calling static function BEFORE creating any objects!
    Employee::showTotalEmployees(); // Prints: 0

    Employee e1, e2;
    e1.setDetails(101, "Aman");
    e2.setDetails(102, "Pooja");

    e1.display();
    e2.display();

    // Calling static function using Class Name
    Employee::showTotalEmployees(); // Prints: 2

    return 0;
}

4. Comparison Table: Regular vs. Static Members

Feature Regular (Non-Static) Member Static Member
Memory Allocation Copied separately inside every object Allocated only once for the entire class
How to Call? Needs an object: obj.display(); Called directly using class name: Class::func();
this Pointer Has an implicit this pointer No this pointer exists
What can it access? Can access both static and regular variables Can ONLY access static variables
Where to Initialize? Usually inside constructors Must be defined outside the class using ::

DAVV Exam Preparation Tips

Frequently Asked 5-Mark Questions:

  1. What is a static data member? Why must it be defined outside the class?
    Model Answer:
    • A static data member is a shared class variable with only one copy for all objects.
    • It must be defined outside the class using int ClassName::varName = 0; because declaring it inside only informs the compiler about its existence; real memory is reserved when defined outside in global memory.
  2. State two key differences between static and non-static member functions in C++.
    Model Answer:
    • Static functions can be called without creating an object using ClassName::func().
    • Static functions do not have a this pointer and can only access static members.

Frequently Asked 10-Mark Questions:

  1. Explain the purpose, rules, and syntax of static data members and static member functions in C++. Write a complete program to count and display the number of objects created.
    Model Answer:
    • Explain the concept of shared class-wide data vs instance data.
    • Detail rules for static variables (initialization outside class, auto-zeroed).
    • Detail rules for static functions (called via ::, only access static data, no this pointer).
    • Write out the complete Employee or StudentCounter program.