Static in C++





Static is a keyword in C++ that is used to create static variables and static functions. To declare any variable or function we write static before it.Static elements have special characteristics like...

  1. Static elements are allocated storage only once in a program lifetime in static storage area.
  2. They have a scope till the program lifetime.
  3. Static variables are initialized with 0(zero){if not initialized in program}.

Static keyword can be used with the following ...

  1. Static variable in functions.
  2. Static member variable in class.
  3. Static member function in class.

1. Static variable in functions :
  • You can declare variable as static inside function.

  • Static variables when used inside function are initialized only once.

  • The lifetime of static local variable is till program in execution.

  • The scope of static local variable is local to block.

  • They hold their last updated value in multiple function calls.

Code Example:
#include<iostream>
using namespace std;
void counter()
{
 int a = 10;
 static int c = 1;
 c++;
 a++;
 cout<<"a = "<< a<< endl;
 cout<<"c = "<< c<< endl;
}
int main() 
{
 counter( );
 counter( );
 counter( );
 return 0;
}                
OUTPUT:
a = 10
c = 1
a = 10
c = 2
a = 10
c = 3 

As you can see in the above example there are two variable one is static and another is non-static. Static variable 'c' initialized only once while non-static variable 'a' get initialize in every function call.


2. Static Members of Class :


In a class you can declare two type of members(variables and functions)
1. Static Members
2. Non-Static Members
By default all members are non-static in class, if you want to declare any member as static just use the keyword static before that member.

1. Static Data Members of Class :
  • The variable inside the class declared with static modifier are called static or class data members.

  • Unlike non-static data members that are separately available for all objects, static data members of class has a single copy and shared by all objects.

  • All static data is initialized to zero when the first object is created, if no other initialization is present.

  • Note: You can't initialize static data member in class definition ,it must be initialized explicitly always outside of the class using scope resolution operator(::) as in the below example.

Code Example
#include<iostream>
using namespace std; 
class Test 
{  
public:
 static int a,b;
 void show()
 {
  cout<<"a = "<< a<< endl;
  cout<<"b = "<< b<< endl;
  a++;
  b++;
 }
};  
//initializing with 1 
int Test::a=1;

//implicitly initialize with 0
int Test::b; 

int main() {  
Test ob;
ob.show();
ob.show();
return 0;
}                         
                    
OUTPUT:
a = 1
b = 0
a = 2
b = 1    

2. Static Member Functions of Class :
  • The functions inside the class declared with static modifier are called static or class member functions.

  • These functions work for the class as whole rather than for a particular object of class.

  • To call a static member functions of class object is not required, you can call a static member function using class name with SRO (::),
    Syntax:
    ClassName::functionName();

  • Static member function can access only static members of class, to access non-static members object is required.

  • Note: You can't use this pointer in static function.

Code Example
#include<iostream>
using namespace std;
class Test
{
 int x;
 static int a;
 public:
 static void message( ) 
 {
  cout<<"Hello INDIA"<< endl;
//ERROR can't access non-static
  cout<<"x = "<< x<< endl;
  cout<<"a = "<< a<< endl;
 }
void greet( )
 {
  cout<<"Good Morning!!"<< endl;
 }
};
int Test::a;
int main()
{
//static function call	
Test::message( );
Test ob;
ob.greet( );
return 0;
} 

Next topic is New and Delete in C++





 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved