Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count the number of objects using Static member function

  • Difficulty Level : Medium
  • Last Updated : 29 Mar, 2020

Prerequisite : Static variables , Static Functions
Write a program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class.

Explanation:
In this program we are simply explaining the approach of static member function. We can define class members and member functions as static using static keyword. Before understanding static member function, we must understand static member. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.

Important points about Static :

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • A static member is shared by all objects of the class, all static data is initialized to zero when the first object is created, if no other initialization is present.
  • A static member function can only access static data member, other static member functions and any other functions from outside the class.
  • By declaring a function member as static, we make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator :: .
  • We can’t put it in the class definition but it can be initialized outside the class as done in the following example by re-declaring the static variable, using the scope resolution operator :: to identify which class it belongs to.

Examples:

Input : Here  we are not asking for input from the user
Output :count:2
count:3
object number :1
object number :2
object number :3

Input :Here we are not asking for input from the user
Output :count:2
count:3
object number :1
object number :2
object number :3




// C++ program to Count the number of objects
// using the Static member function
#include <iostream>
using namespace std;
class test {
    int objNo;
    static int objCnt;
  
public:
    test()
    {
    objNo = ++objCnt;
    }
    ~test()
    {
    --objCnt;
    }
    void printObjNumber(void)
    {
        cout << "object number :" << objNo << "\n";
    }
    static void printObjCount(void)
    {
        cout << "count:" << objCnt<< "\n";
    }
};
int test::objCnt;
int main()
{
    test t1, t2;
    test::printObjCount();
  
    test t3;
    test::printObjCount();
  
    t1.printObjNumber(); 
    t2.printObjNumber(); 
    t3.printObjNumber();
    return 0;
}


Output:

count:2
count:3
object number :1
object number :2
object number :3



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!