C++

C++ : static keyword

Need for static data members
There are times when it is simply more efficient to provide only a single copy of a class variable between all the objects of a class, rather than having each object maintain its own copy. Below are some of the uses of static data members :

  • A static data member of a class could be used to keep track of the number of class objects that are created.
  • A static data member could be a pointer pointing to the error-handling or logging function for that class.

Accessing a static data members

  • If a static data member is declared inside the public section of the class, then it can be accessed outside the class using ClassName :: static_member;
  • If a static data member is declared inside the private section of the class, then it can be accessed outside the class using
    • non-static member functions.
    • static member functions.

Defining a static data members

  • Static data member must be definied in a .cpp file. If this is not done a linked error is reported.
Static Data Members Static Member Functions
- Data to be shared by all objects of a class, is stored in static data members. Thus static variable is a class variable.
- Only a single copy of the static data member exists.
- Static data member could be defined inside the public or the private section of a class.
- For declaration a static data member is preceded with the keyword static.
- Static member functions can only access static data members.
A static member function is called using the class name i.e Class_Name :: static_function
- this pointer never gets passed to a static member function.
- As static member functions never receive the this pointer, they cannot access non-static data members.

Below C++ program demonstrates the use of static data members and static member functions of a class.
Class definition : Employee.h

#include<iostream>
#include<string>

class Employee {

    private:
    int m_emp_id;
    std :: string m_emp_name;
    static int m_company_code;

    public:
    static std :: string m_company;
    Employee (int id, std :: string name) : m_emp_id (id), m_emp_name(name)
    {}  
    void Employee_Details();
    static void Org_Details();
};

Employee.cpp

#include "class.h"

std :: string Employee :: m_company = "Easy";
int Employee :: m_company_code = 12320;

void Employee :: Employee_Details() {
    std :: cout << "Employee Id : " << m_emp_id << std :: endl;
    std :: cout << "Employee Name : " << m_emp_name << std :: endl;
}

void Employee :: Org_Details() {
    std :: cout << "Organization Code : " << m_company_code << std :: endl;
    std :: cout << "Organization Name : " << m_company << std :: endl;
}

int main() {

    Employee e(25, "Deltoid");

    e.Employee_Details();
    Employee :: Org_Details();
        
    std :: cout << "Organization Name : " << Employee :: m_company << std :: endl;
    return 0;
}

Output

Employee Id : 25
Employee Name : Deltoid
Organization Code : 12320
Organization Name : Easy
Organization Name : Easy


Copyright (c) 2019-2021, Algotree.org.
All rights reserved.