---
title: "Program using static members"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:static-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/static-program
site: BCT Engineering
---

# Program using static members

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2068 Baisakh, 6 marks
- Video: [Static Member Functions | C++ Tutorial](https://www.youtube.com/watch?v=1xY0rF399j0) — Portfolio Courses
- 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-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Write a program that uses static member functions and static data member.

## Short answer

The program uses a static data member count to track number of Employee objects and a static total for salary sum, initialized outside the class. A static member function showSummary() displays these, called with class name. Example: after creating three employees, it shows Employees = 3, Total salary = 70000.

## Answer

The program counts the number of objects created using a static data member and displays the count with a static member function. It also keeps a static total of the salary of all employees.

```cpp
#include <iostream>
using namespace std;

class Employee
{
    int id;
    float salary;
    static int count;              // number of objects
    static float total;            // total salary of all
public:
    Employee(float s)
    {
        id = ++count;
        salary = s;
        total += s;
    }
    void display()
    {
        cout << "Employee " << id << " salary = " << salary << endl;
    }
    static void showSummary()      // static member function
    {
        cout << "Employees = " << count
             << ", Total salary = " << total << endl;
    }
};

int Employee::count = 0;           // definitions of static members
float Employee::total = 0;

int main()
{
    Employee::showSummary();       // call without an object
    Employee e1(20000), e2(35000), e3(15000);
    e1.display();
    e2.display();
    e3.display();
    Employee::showSummary();
    return 0;
}
```

#### Output

```text
Employees = 0, Total salary = 0
Employee 1 salary = 20000
Employee 2 salary = 35000
Employee 3 salary = 15000
Employees = 3, Total salary = 70000
```

#### Points

- `count` and `total` have a single copy shared by all objects.
- `showSummary()` is static, so it can use only static members and is called with `Employee::showSummary()`.
- Static members must be defined outside the class.

## Check yourself

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

1. How does the program count Employee objects?
   A. Constructor increments static count
   B. Main increments a local variable
   C. Destructor increments count
   D. Each object has its own count
   Answer: A. The constructor does ++count on a single shared static member. (Key point: Counting objects)

2. What does the output 'Employees = 3, Total salary = 70000' come from?
   A. 20000+35000+15000 added into static total
   B. 3 x 20000
   C. Average salary
   D. Last salary entered
   Answer: A. Each constructor adds its salary to static total: 70000. (Key point: Static total)

3. Where are int Employee::count = 0; and float Employee::total = 0; written?
   A. Inside the class
   B. Outside the class
   C. Inside showSummary
   D. Inside the constructor
   Answer: B. Static members must be defined outside the class. (Key point: Defining statics)

4. What can showSummary(), a static function, use?
   A. Only static members
   B. Any non-static member
   C. Only id
   D. this pointer
   Answer: A. A static function has no this pointer, so it uses only static members, and is called as Employee::showSummary(). (Key point: Static function limits)

## Related in Object Oriented Programming

- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Access overridden members from derived class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aaccess-overridden-members) — Object Oriented Programming, chapter 6, asked 2×

## 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×
- [Truss joint B: temperature and vertical members long](https://www.bctengineer.com/question.md?key=semester_4%2Ftheory_of_structures_i%3Atruss-defl-b-temp-lackfit-2073) — Theory of Structures I, chapter 3, asked 2×
