Implement a Program to Demonstrate Static Data Members and Static Functions in C++
In C++, the `static` keyword can be applied to both data members and member functions within a class. These static members are shared by all objects of the class, rather than each object having its own copy. This concept is extremely useful when you want a common value or behavior across all instances of a class.
In this blog, we’ll learn:
- What static data members are
- What static member functions are
- How to implement and use them in a C++ program
🔹 What are Static Data Members?
A static data member of a class is shared among all objects of the class. Unlike normal (non-static) data members, which are unique to each object, static members are allocated only once in memory and retain their value across objects.
🔹 What are Static Member Functions?
A static member function can access only static data members or other static functions. It is invoked using the class name rather than an object of the class.
A static member function can access only static data members or other static functions. It is invoked using the class name rather than an object of the class.
✅ C++ Program: Demonstrate Static Data Members and Static Functions
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
string name;
static int studentCount; // Static data member
public:
// Constructor
Student(string n, int r) {
name = n;
rollNo = r;
studentCount++; // Increment count when object is created
}
// Display student details
void display() {
cout << "Name: " << name << ", Roll No: " << rollNo << endl;
}
// Static function to get student count
static void showCount() {
cout << "Total number of students: " << studentCount << endl;
}
};
// Definition and initialization of static member
int Student::studentCount = 0;
// Main function
int main() {
Student::showCount(); // Calling static function without object
Student s1("Vivek", 101);
Student s2("Anjali", 102);
Student s3("Rohan", 103);
s1.display();
s2.display();
s3.display();
Student::showCount(); // Static function to display total count
return 0;
}
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
string name;
static int studentCount; // Static data member
public:
// Constructor
Student(string n, int r) {
name = n;
rollNo = r;
studentCount++; // Increment count when object is created
}
// Display student details
void display() {
cout << "Name: " << name << ", Roll No: " << rollNo << endl;
}
// Static function to get student count
static void showCount() {
cout << "Total number of students: " << studentCount << endl;
}
};
// Definition and initialization of static member
int Student::studentCount = 0;
// Main function
int main() {
Student::showCount(); // Calling static function without object
Student s1("Vivek", 101);
Student s2("Anjali", 102);
Student s3("Rohan", 103);
s1.display();
s2.display();
s3.display();
Student::showCount(); // Static function to display total count
return 0;
}
🔍 Output Explanation
Total number of students: 0
Name: Vivek, Roll No: 101
Name: Anjali, Roll No: 102
Name: Rohan, Roll No: 103
Total number of students: 3
Total number of students: 0
Name: Vivek, Roll No: 101
Name: Anjali, Roll No: 102
Name: Rohan, Roll No: 103
Total number of students: 3
- Before creating any objects, `studentCount` is 0.
- After creating 3 student objects, the count becomes 3.
- The static data member `studentCount` keeps track of the number of students.
- The static function `showCount()` accesses this shared data and is called using the class name, not an object.
Static data members and static functions provide a powerful mechanism to manage shared data across all objects of a class in C++. They are often used to:
- Track the number of objects created
- Share configuration or constants
- Group utility functions within classes
Using static members efficiently can lead to cleaner and more memory-optimized code in C++. Practice these concepts by modifying the example — try adding a function to reset the count, or create more student objects dynamically.
Comments
Post a Comment