Aim:
To implement static member function in class.
Algorithm:
- Create a class test with static data member as count.
- Create a member function to increment the count.
- Declare the static data member using the scope resolution operator.
- Display the count value.
Program:
#include <iostream> using namespace std; class MyClass{ private: static int a; public: MyClass() { a++; } static int print() { return a; } }; int MyClass::a = 0; int main() { MyClass A,B,C,D; cout << "Number of objects: " << MyClass::print(); }
Execution:
Number of objects: 4
Result:
Thus, the Implementation static member function in class was executed successfully.