Static Member in C++ Language
Static Member in C++
There are three types of Static Member in C++ Language:-
1- Static Local Variables2- Static Member Variables
3- Static Member Functions
1- Static Local Variables
Example of Static Local Variables
static local variable |
Here in above example,'static int x' means static local variable which by default value will be zero and 'int y' by default value will be garbage.When fun() is call then 'y' variable get value
and after the work of fun() finished then the value of 'y' will destroy but the variable of 'x'
get value from the beginning of program which remains until the end of the program.
2- Static Member Variables
Example of Static Member Variables
static member variable |
In above example , 'int balance' is called Instance Member Variable where as static float roi(rate
of interest) is called Static Member Variable or we can say Class Variable. When we make 'Account'
class object -a1,a2 which has only one variable i.e int balance and 'roi' variable is not the part of object therefore it's exit whether you made any object like a1,a2 or not but you have to declared
'roi' variable outside the class as we seen in above example..
3- Static Member Functions
Here in above example, we can't access 'roi' variable in 'main ()' because it is private member so if want to access' roi' in 'main ()' function then we have to make function in class i.e 'void setRoi(float r)' where r recieve float type value as we seen in above example but remember here 'setRoi()' function access by an object (a1) of class(Account) but we know that static member call directly without any object so we have to make' setRoi ()' function as Static Member Function as we see in below:--
Here we seen that to make setRoi as Static Member Function, we used Static keyword before
setRoi() function as 'Static void setRoi (float r)' . Now, we can directly access 'setRoi()' function
in 'main ()' function i.e Account(class name)::(scope revolution)setRoi(4.5f)(function name) of as we seen in above..
If this article is helpful to you then please share with friends and college ... thank you..
static member function |
Here we seen that to make setRoi as Static Member Function, we used Static keyword before
setRoi() function as 'Static void setRoi (float r)' . Now, we can directly access 'setRoi()' function
in 'main ()' function i.e Account(class name)::(scope revolution)setRoi(4.5f)(function name) of as we seen in above..
If this article is helpful to you then please share with friends and college ... thank you..
Comments
Post a Comment