Static members in class C++

C++ Static members in a class 

Why static members used in classes?


The static members in class used to declare having members that value do not change when a new object created. For example, we need to count how many objects create then a static member used or needed information about the previous object.


How can used static members in classes c++?

We can used static keyword to define static members of the class.A static member of the class shared by all the objects of the class. During the initialization of the static member is initialized by zero.



 Take an example to understand the concept of the static members in classes c++.


For example:

#include<iostream>


using namespace std;


class myclass //create a class


{


private:


int number ;

char character ;

float decimal ;

public:

static int obj ;

myclass(int num,float deci ,char cha )


{

decimal=deci;

character = cha;

number = num ;

obj++; //increment in the static variable when constructor called.

cout<<" constructor "<<endl;

}


void display() //display function


{


cout<<" number ="<< number <<endl  ;

cout<<" character = "<<character<<endl ;

cout<<" decimal = "<<decimal<< endl ;

}


int objectcount()

{

return obj; //return the static variable.

}


};


int myclass::obj=0;

//initialize the static variable outside the class



int main()


{


myclass ob(5,5.5,'h'); //create the object



myclass cal(60,69.99,'h'); //create another object

cout<<" object counts = "<<cal.objectcount(); // call the object count function.



return 0;


}



Here, the static variable of the class myclass count the objects of the class. This is due to the value of the static variable that does not change once one object of the class is created until the rest of the program.
Objectcount function is used to display the object counts.In the constructor of the class increment in the static variable obj. Every time constructor call means object creates then increment in the obj  variable.

Static function in the class in C++

The static function is only made to access and return the static data or static function of the class or the program. Nonstatic data is not allowed to used in the static function .we can call the static member function even no object is created with help of class name and scope resolution.
Lets, try an example to make the clarification about the static member function in the c++ classes.

For example:

#include<iostream>


using namespace std;


class myclass //create a class


{


private:


int number ;

char character ;

float decimal ;

public:

static int obj ;

myclass(int num,float deci ,char cha )


{

decimal=deci;

character = cha;

number = num ;

obj++; //increment in the static variable when constructor called.

cout<<" constructor "<<endl;

}


void display() //display function


{


cout<<" number ="<< number <<endl  ;

cout<<" character = "<<character<<endl ;

cout<<" decimal = "<<decimal<< endl ;

}


static int objectcount()

{

return obj; //return the static variable.

}


};


int myclass::obj=0;

//initialize the static variable outside the class



int main()


{


myclass ob(5,5.5,'h'); //create the object



myclass cal(60,69.99,'h'); //create another object

cout<<" object counts = "<<cal.objectcount(); // call the object count function.


return 0;



}




Here, instead of the simple function static function is used to return the static variable obj of the class myclass. Otherwise same example is used in the above . Remember that the static member function of the class is only used static data of the class otherwise error will have happened.


Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

how to cout in cpp

input by cin in c++

Flutter layout