Search Your Topic

config
Tuesday, 13 June 2017 12:45

C++ static

Written by
Rate this item
(0 votes)

In C++, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C++, static can be field, method, constructor, class, properties, operator and event.


Advantage of C++ static keyword

Memory efficient: Now we don't need to create instance for accessing the static members, so it saves memory. Moreover, it belongs to the type, so it will not get memory each time when instance is created.


C++ Static Field

A field which is declared as static is called static field. Unlike instance field which gets memory each time whenever you create object, there is only one copy of static field created in the memory. It is shared to all the objects.

It is used to refer the common property of all objects such as rateOfInterest in case of Account, companyName in case of Employee etc.


C++ static field example

Let's see the simple example of static field in C++.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Account {  
  4.    public:  
  5.        int accno; //data member (also instance variable)      
  6.        string name; //data member(also instance variable)  
  7.        static float rateOfInterest;   
  8.        Account(int accno, string name)   
  9.         {    
  10.              this->accno = accno;    
  11.             this->name = name;    
  12.         }    
  13.        void display()    
  14.         {    
  15.             cout<<accno<< "<<name<< " "<<rateOfInterest<<endl;   
  16.         }    
  17. };  
  18. float Account::rateOfInterest=6.5;  
  19. int main(void) {  
  20.     Account a1 =Account(201, "Sanjay"); //creating an object of Employee   
  21.     Account a2=Account(202, "Nakul"); //creating an object of Employee  
  22.     a1.display();    
  23.     a2.display();    
  24.     return 0;  
  25. }  

Output:

201 Sanjay 6.5
202 Nakul 6.5

C++ static field example: Counting Objects

Let's see another example of static keyword in C++ which counts the objects.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Account {  
  4.    public:  
  5.        int accno; //data member (also instance variable)      
  6.        string name;   
  7.        static int count;     
  8.        Account(int accno, string name)   
  9.         {    
  10.              this->accno = accno;    
  11.             this->name = name;    
  12.             count++;  
  13.         }    
  14.        void display()    
  15.         {    
  16.             cout<<accno<<" "<<name<<endl;   
  17.         }    
  18. };  
  19. int Account::count=0;  
  20. int main(void) {  
  21.     Account a1 =Account(201, "Sanjay"); //creating an object of Account  
  22.     Account a2=Account(202, "Nakul");   
  23.      Account a3=Account(203, "Ranjana");  
  24.     a1.display();    
  25.     a2.display();    
  26.     a3.display();    
  27.     cout<<"Total Objects are: "<<Account::count;  
  28.     return 0;  
  29. }  

Output:

201 Sanjay
202 Nakul
203 Ranjana
Total Objects are: 3
Read 178 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ this Pointer C++ Structs »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.