on Leave a Comment

C++ Static Members of Class

Static is a keyword in C++. Static keyword is used to give some more characteristic of a variable or function. Concept of static local variable in C++ is similar to C language. But C++ has some new concepts of static member variable and static member functions that is not available in C language.

Objective

  1. Static local variable
  2. Static member variable
  3. Static member functions
STATIC LOCAL VARIABLE


Static local variable can be defined in a function using static keyword. The initial value of static local variable is by default zero. The scope of static local variable is throughout the lifetime of program.

For example,

#include<iostream>
using namespace std;
void fun();
int main()
{
    fun();
    return 0;
}
void fun()
{
    static int bh;              //Static local variable declaration
    cout<<"Value of bh is "<<bh;
}

OUTPUT:

Value of bh is 0

Here the initial value of bh is zero. Whereas the value of non-static variable is garbage value. Static keyword is necessary to declare a variable as static.

STATIC MEMBER VARIABLE

Static member variables are declared inside the class body. Static member variable are also known as class member variable. Declaration of static member variable is written inside the class but definition of static member variable is written outside the class. Static member variables do not belong to any separate object, but they belong to the whole class. Only one copy of static member variable is made for the whole class. Objects can use the static member variable.

Simple program of understand static member variable

#include<iostream>
using namespace std;
class B
{
    static int a;                      //static member variable declaration
    int b;
public:
    void setdata(int x)
    {
        b=x;
    }
};
int B:: a;                                //static member variable definition
int main()
{
    B obj;
    return 0;
}

Here a is a static member variable. As you can see above declaration of static member variable is written inside the class but definition of static member variable is written outside the class. Static member variable can be accessed using static member function.

If we declare static member variable as public inside the class. Then we can access static member variable using object and class name in main function. For example,

#include<iostream>
using namespace std;
class B
{
    public:
    static int a;
    int b;
public:
    void setdata(int x)
    {
        b=x;
    }
};
int B:: a;
int main()
{
    B obj;
    cout<<obj.a;
    cout<<endl;
    cout<<B::a;
    return 0;
}

OUTPUT:

0
0

STATIC MEMBER FUNCTION

Static member function is defined using static keyword. Static member function is usually used to access static member variable. Static member functions can be called by objects or by class.

An example to understand static member functions

#include<iostream>
using namespace std;
class B
{
    static int A;
    int b;
public:
    void setdata(int x)
    {
        b=x;
    }
    static void setA(int x)
    {
        A=x;
    }
    static void putA()
    {
        cout<<"\n"<<A;
    }
};
int B:: A;
int main()
{
    B obj;
    obj.setA(5);  //Access using object
    obj.putA();  
    B::setA(10);  //Access using class
    B::putA();
    return 0;
}

OUTPUT:

5
10

You can access static member function by either class name or object name. 

0 comments:

Post a Comment