Count the number of objects using Static member function Last Updated : 29 Mar, 2020 Comments Improve Suggest changes Like Article Like Report 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 : 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 CPP // 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 Comment More infoAdvertise with us Next Article Count the number of objects using Static member function P prashant dhyani Follow Improve Article Tags : Misc C Language C++ C++-Static Keyword Static Keyword +1 More Practice Tags : CPPMisc Similar Reads Static Member Function in C++ The static keyword is used with a variable to make the memory of the variable static once a static variable is declared its memory can't be changed. To know more about static keywords refer to the article static Keyword in C++. Static Member in C++ Static members of a class are not associated with t 4 min read C# | Get the number of elements contained in the Stack Stack represents a last-in, first out collection of object. Stack<T>.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack<T> Return Value: The 2 min read C# | Count the number of key/value pairs in the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable. Syntax: myTable. 2 min read unordered_set count() function in C++ STL The unordered_set::count() function is a built-in function in C++ STL which is used to count occurrences of a particular element in an unordered_set container. As the unordered_set container does not allows to store duplicate elements so this function is generally used to check if an element is pres 2 min read Some interesting facts about static member functions in C++ 1) static member functions do not have this pointer. For example following program fails in compilation with error "`this' is unavailable for static member functions " CPP #include<iostream> class Test { static Test * fun() { return this; // compiler error } }; int main() { getchar(); return 0 1 min read set::count() Function in C++ STL The std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.ExampleC 3 min read Memory Allocation in Static Data Members in C++ C++ allows defining static data members within a class using the static keyword. When a data member is declared as static, then we must keep the following note in mind: Irrespective of the number of objects created, only a single copy of the static member is created in memory. All objects of a class 4 min read C# | Get the number of elements in the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Count Property is used to get the number of elements in the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elemen 2 min read unordered_multiset count() function in C++ STL The unordered_multiset::count() is a built-in function in C++ STL which returns the count of elements in the unordered_multiset container which is equal to a given value. Syntax: unordered_multiset_name.count(val) Parameters: The function accepts a single mandatory parameter val which specifies the 2 min read unordered_set bucket_count() function in C++ STL The unordered_set::bucket_count() method is a builtin function in C++ STL which returns the total number of buckets present in an unordered_set container. The bucket is a slot in the unordered_set's internal hash table where elements are stored. Note: Buckets in unordered_set are numbered from 0 to 2 min read Like