Static Keyword In C++

Share Your Love

Static Keyword In C++ is also called a modifier that refers to the type rather than the instance. As a result, access to the static members does not need the use of an instance. Field, method, constructor, class, properties, operator, and event are all examples of static in C++.

Advantage of static keyword In C++:

It saves memory since we no longer need to construct an instance to access the static members. Furthermore, because it is a type, it will not be allocated memory each time an instance is created.

C++ Static Field:

A static field is one that has been declared as static. Unlike instance fields, which receive memory each time an object is created, static fields have just one copy in memory. It is accessible to all things.

Let’s talk about an example below we go through an example of common property of all objects such as rateOfInterest in the case of Account class.

Now, the example:

C++ static field example:

Let’s see the simple example of a static field in C++,

#include <iostream>  
using namespace std;  
class Account {  
   public:  
       int accno; //data member (also instance variable)      
       string name; //data member(also instance variable)  
       static float rateOfInterest;   
       Account(int accno, string name)   
        {    
             this->accno = accno;    
            this->name = name;    
        }    
       void display()    
        {    
            cout<<accno<< "<<name<< " "<<rateOfInterest<<endl;   
        }    
};  
float Account::rateOfInterest=6.5;  
int main(void) {  
    Account a1 =Account(201, "Rahul"); //creating an object of Employee   
    Account a2=Account(202, "Shyam"); //creating an object of Employee  
    a1.display();    
    a2.display();    
    return 0;  
} 

Output:

201 Rahul 6.5
202 Shyam 6.5

C++ static field example: Counting Objects

Let’s look at another use of the static keyword in C++: counting the number of objects:

#include <iostream>  
using namespace std;  
class Account {  
   public:  
       int accno; //data member (also instance variable)      
       string name;   
       static int count;     
       Account(int accno, string name)   
        {    
             this->accno = accno;    
            this->name = name;    
            count++;  
        }    
       void display()    
        {    
            cout<<accno<<" "<<name<<endl;   
        }    
};  
int Account::count=0;  
int main(void) {  
    Account a1 =Account(201, "Rahul"); //creating an object of Account  
    Account a2=Account(202, "Shyam");   
     Account a3=Account(203, "Krishna");  
    a1.display();    
    a2.display();    
    a3.display();    
    cout<<"Total Objects are: "<<Account::count;  
    return 0;  
}  

Output:

201 Rahul
202 Shyam
203 Krishna
Total Objects are: 3

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter