0% found this document useful (0 votes)
6 views3 pages

Understanding Static Members in C++

In C++, static members are class-level variables and functions shared among all objects of a class, not tied to any specific instance. They are declared with the static keyword, defined outside the class for memory allocation, and can be accessed using either the class name or an object name. Static member functions can only access static data members, and memory for static variables is allocated only once, regardless of the number of objects created.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Understanding Static Members in C++

In C++, static members are class-level variables and functions shared among all objects of a class, not tied to any specific instance. They are declared with the static keyword, defined outside the class for memory allocation, and can be accessed using either the class name or an object name. Static member functions can only access static data members, and memory for static variables is allocated only once, regardless of the number of objects created.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Static Data Members and Static Member

Functions in C++
Definition

In C++, the static keyword is used to create class-level members (both variables and functions)
that are shared by all objects of the class.

Unlike normal (non-static) members:

 Static members are not tied to any specific object.


 They belong to the class itself, not individual objects.
 All objects share the same copy of a static variable.

Key Points:

1. Declared using the keyword static inside the class.


2. Defined outside the class (for memory allocation).
3. Shared among all objects.
4. Can be accessed using class name or object name.
5. Static member functions can access only static data members.
6. Memory is allocated only once, even if multiple objects are created.

Program: Demonstrating Static Members


#include <iostream>
using namespace std;

class Student {
private:
string name;
int rollNo;

// 🔹 Static data member


static int count;

public:
// 🔹 Constructor
Student(string n) {
name = n;
rollNo = ++count; // Increment count each time an object is created
}

// 🔹 Static member function


static int getCount() {
return count;
}
void display() {
cout << "Student Name: " << name << "\tRoll No: " << rollNo << endl;
}
};

// 🔹 Definition of static data member (outside class)


int Student::count = 0;

int main() {
cout << "------------------------------------------\n";
cout << " STATIC MEMBER DEMONSTRATION\n";
cout << "------------------------------------------\n";

Student s1("Bhupendra");
Student s2("Amit");
Student s3("Rohit");

[Link]();
[Link]();
[Link]();

cout << "------------------------------------------\n";


cout << "Total Students Created (using class name): "
<< Student::getCount() << endl;
cout << "------------------------------------------\n";

return 0;
}

Output
------------------------------------------
STATIC MEMBER DEMONSTRATION
------------------------------------------
Student Name: Bhupendra Roll No: 1
Student Name: Amit Roll No: 2
Student Name: Rohit Roll No: 3
------------------------------------------
Total Students Created (using class name): 3
------------------------------------------

Explanation:
Concept Description
Static Data
count keeps track of the total number of students. It’s shared by all objects.
Member
Definition Outside int Student::count = 0; is mandatory to allocate memory for the static
Class variable.
Incrementing Every time the constructor runs, count is incremented.
Concept Description
Count
getCount() returns the same value for all objects. It can be accessed
Static Function
without creating any object using Student::getCount().
Memory Sharing Only one copy of count exists, even though there are three objects.
You can use either Student::getCount() or [Link](), but using
Access Methods
class name is preferred.

When to Use Static Members:


✅ When you want a common property shared by all objects (like total count, interest rate,
configuration value, etc.).
✅ To implement object counters, singletons, or class-level constants.

Summary
Type Belongs To Memory Created Access Type Example
Non-static Member Each object Per object Object only [Link]()
Static Member Class Once only Class or Object Student::getCount()

You might also like