---
title: "Static data member and member function"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:static-member-function"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/static-member-function
site: BCT Engineering
---

# Static data member and member function

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2074 Asoj, 5 marks; 2073 Shrawan, 4 marks; 2068 Baisakh, 2+2 marks (asked 3 times)
- Video: [Static Data Members in C++ & Static Member Functions in C++|C++ programming tutorials for Beginners](https://www.youtube.com/watch?v=1QTZeeDL0bc) — Simple Snippets
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/static-member-function
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

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.

## 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().

## Answer

#### 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:

```cpp
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

```cpp
#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

```text
Objects created = 0
Roll = 1
Roll = 3
Objects created = 3
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How many copies of a static data member exist for a class?
   A. One per object
   B. One per member function
   C. Exactly one shared by all objects
   D. None until an object is created
   Answer: C. A static data member has a single copy shared by all objects. (Key point: Static data member sharing)

2. Where must a static data member be defined?
   A. Inside the constructor
   B. Outside the class using the scope resolution operator
   C. Only inside main()
   D. Inside the static function
   Answer: B. It is declared in the class and defined outside, e.g. int Counter::count = 0; (Key point: Defining static member)

3. Why can a static member function access only static members?
   A. It is private
   B. It has no this pointer
   C. It is called with an object
   D. It is defined outside the class
   Answer: B. A static function has no this pointer, so it cannot reach non-static data. (Key point: Static function limits)

4. How can showCount() be called before any object exists?
   A. s1.showCount()
   B. Student::showCount()
   C. showCount(Student)
   D. Not possible
   Answer: B. Static functions are called using the class name with ::, with no object needed. (Key point: Calling static function)

## Related in Object Oriented Programming

- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4
- [Program using static members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-program) — Object Oriented Programming, chapter 4
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4
- [Overloading via member and non-member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-vs-nonmember-overload) — Object Oriented Programming, chapter 5
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×

## Related in other subjects

- [Static and dynamic data structures; array list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-dynamic-ds-array-list) — Data Structure and Algorithm, chapter 3
- [Functions of data link layer](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Adll-functions) — Computer Networks and Security, chapter 3, asked 3×
- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
- [Short note: Discrete data function](https://www.bctengineer.com/question.md?key=semester_4%2Fsignals_and_systems%3Ashort-note-discrete-data-function) — Signals and Systems, chapter 1
