Skip to main content

Simple Program for Static Data and Member Function

1 min read Updated June 30, 2026
Share:
On this page (7sections)

Aim

To count the object value using the storage keyword static.

Static Data and Member Function Algorithm/Steps:

  • STEP 1: Start the program.
  • STEP 2: Declare the class name as Stat with data members and member functions.
  • STEP 3: The constructor Stat() which is used to increment the value of count as 1 to assign the variable code.
  • STEP 4: The function show code() to display the code value.
  • STEP 5: The function showcount() to display the count value.
  • STEP 6: Stop the program.

Simple Program for Static Data and Member Function

#include<iostream.h>
#include<conio.h>

class stat {
    int code;
    static int count;

public:

    stat() {
        code = ++count;
    }

    void showcode() {
        cout << "\n\tObject number is :" << code;
    }

    static void showcount() {
        cout << "\n\tCount Objects :" << count;
    }
};

int stat::count;

void main() {
    clrscr();
    stat obj1, obj2;

    obj1.showcount();
    obj1.showcode();
    obj2.showcount();
    obj2.showcode();
    getch();
}

Sample Output

Count Objects: 2
Object Number is: 1
Count Objects: 2
Object Number is: 2

How It Works

This C++ program demonstrates Simple Program for Static Data and Member Function. It first prepares the data it needs, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Learn the concept first, then study the code:

Frequently Asked Questions

What does this C++ program do?
It is a C++ example program that demonstrates Simple Program for Static Data and Member Function, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses user-defined functions, illustrating a common pattern in C++ programming.

Related Tutorials

Search tutorials