Static Data Members & Static Member Functions in C++
Each library card object can store its own card number while the class shares one static count. A static member function can read that count without a particular card. It needs an object to inspect any individual card number.
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!
An instance field can differ between two objects. A static data member is shared by instances of the class.
- Object 1 (emp1) — id = 101, salary = 40000
- ONE SHARED STATIC VARIABLE — static int totalCount = 2 (Stored only once in Global Memory)
- Object 2 (emp2) — id = 102, salary = 55000
Key Rules for Static Data Members:
- Single Copy: Created only once when the program starts.
- Auto-Zero: If you don’t give it any initial value, C++ automatically sets it to zero (
0). - 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.
View diagram · Instance Variables vs. Shared Static Class Variable
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):
- 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! - Access Restrictions: A static function can ONLY access static variables or other static functions. It CANNOT touch normal non-static variables (like
idorname)! - No
thisPointer: Because a static function doesn’t belong to any particular object, it has nothispointer.
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:
- 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.
- 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
thispointer and can only access static members.
- Static functions can be called without creating an object using
Frequently Asked 10-Mark Questions:
- 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, nothispointer). - Write out the complete
EmployeeorStudentCounterprogram.