- Asked 3/10
- 2074 Asoj · 5 marks
- 2073 Shrawan · 4 marks
- 2068 Baisakh · 2+2 marks
Static data member and member function
Short answer
Static data member is shared by all objects, declared with static inside class and defined outside. Static member function can access only static members and is called via class name without object. Used for class-wide data like object count. Example: static int count; static void showCount(); calls Student::showCount().
What do you understand by static data member and member functions? When do we use static data member and static function in a class? Explain their use in the program with examples.
Static data member
A static data member is declared with the keyword static inside a class. Only one copy exists and it is shared by all objects of the class, instead of each object having its own copy. It is created once, before any object, and initialised to zero by default. It must be defined outside the class using the scope resolution operator:
int Counter::count = 0;
Static member function
A static member function is declared with static. It can access only static data members (and other static functions), because it has no this pointer. It can be called without any object, using the class name: ClassName::function().
When to use
- Static data member: when data belongs to the whole class, such as a count of objects created, a total, or a shared constant (interest rate).
- Static function: when we need to work on static data without creating an object, e.g. to display the object count.
Example
#include <iostream>
using namespace std;
class Student
{
int roll;
static int count; // shared by all objects
public:
Student() { roll = ++count; }
static void showCount() // static function
{
cout << "Objects created = " << count << endl;
}
void show() { cout << "Roll = " << roll << endl; }
};
int Student::count = 0; // definition of static member
int main()
{
Student::showCount(); // 0, no object needed
Student s1, s2, s3;
s1.show();
s3.show();
Student::showCount(); // 3
return 0;
}
Output
Objects created = 0
Roll = 1
Roll = 3
Objects created = 3
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Member function as friend of another class · Object Oriented Programming, chapter 4
- Program using static members · Object Oriented Programming, chapter 4
- Abstract Student class program · Object Oriented Programming, chapter 7
- LandMeasure class and sum function · Object Oriented Programming, chapter 4
- Overloading via member and non-member function · Object Oriented Programming, chapter 5
- Class and function template with syntax · Object Oriented Programming, chapter 9 · asked 4×
- Static and dynamic data structures; array list · Data Structure and Algorithm, chapter 3
- Functions of data link layer · Computer Networks and Security, chapter 3 · asked 3×
- I/O functions used with data files · Computer Programming, chapter 10 · asked 2×
- Short note: Discrete data function · Signals and Systems, chapter 1