So this is something new that is language and purely that is c++ so we are going to study about some theory and program mix .So this is starting there are many parts of this so today study about static variable and static function and a program of it which you can run on turboc++ for others code might be change for some few things so that you can replace so now we start
Static variable
-> These are the variables which keep modify their values in different function call.
-> They are allocated storage only once a time in a program.
-> When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
Static function
-> These are the function which are associated with class not with instance of class .
-> To call static function no need to create objects.
-> There only allowed to be access by static variable.
-> There is a use of scope resolution operator.
CODE
#include<iostream.h> #include<conio.h> class abc
{
static int i;
static int cout;
public:(so it can be access by out of class also)
static void input( )
{
cout<<"enter any number:";
cin>>i;
count++;
}
static void display( )
{
cout<<"i="<<i<<endl;
cout<<"cout="<<cout<<endl;
}
};
int abc :: i;
int abc :: count;
void main( )
{ abc :: input( );
abc :: display( );
abc :: input( );
abc :: display( );
(it is repeated because we want to test that the value of count is increasing or not)
getch( );
}
code finished
NOTE : the text in the ( )these two are not a part of code it is just for your explanation
0 comments:
Post a Comment