Static Member Function Example in C++


Description:

By declaring a function member as static, you 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 ::.

Example Program:

Here is the simple program static member function in C++.

Source Code

#include<iostream>
//Static Member Variable
using namespace std;

class Test
{

private :
    int a,b;
public:
    static int count;
    Test()
    {
        a=10;
        b=10;
        count++;
    }
    static int getCount()  //Static Member function returns only static variables
    {
        return count;
    }
};
int Test::count=0;
int main()
{
    Test t1;
    cout<<"T1 :"<<Test::getCount();
}

To download raw file Click Here

Output

T1 :1

Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes