Static concepts in C++





In C++, the static keyword can be used in various contexts within a class to define static members, static member functions, and static local variables.

Here's a breakdown of what can be declared as static within a C++ class:

  1. Static Data Members in C++
  2. Static Member Functions in C++
  3. Static Local Variables (within member functions) in C++


  1. Static Data Members in C++
    1. Static data members are shared among all objects of the class.
    2. They are declared using the static keyword inside the class but are defined outside the class using the class name and the scope resolution operator (::).
    3. They have only one instance shared across all objects of the class.
    4. Static data members can be of any data type, including fundamental types, user-defined types (classes), pointers, or arrays.


    Syntex:
    class MyClass {
    public:
        static int staticDataMember;
    };
    
    int MyClass::staticDataMember = 0; // Definition of static data member
    
    Example:

    Lets develop a C++ program that effectively use static variables within a class to maintain a count across multiple object instance and discuss the significance of static variables in object-oriented programming and compare their behavior with instance variables.

    #include<iostream>
    using namespace std;
    
    class sample {
    public:
        static int count;
        void display();
    };
    
    int sample::count;
    
    void sample::display() {
        ++count;
        cout << "The value of count : " << count << endl;
    }
    
    int main() {
        sample c1, c2, c3;
        c1.display();
        c2.display();
        c3.display();
    }
    
    Output:
    The value of count : 1
    The value of count : 2
    The value of count : 3
    

    The count variable is declared as static within the class sample. The static variable count is initialized outside the class, with no initial value specified, so it defaults to 0. The display() function increments the static variable count and displays its value. The main() function creates three objects of class sample and invokes the display() function on each object.



  2. Static Member Functions in c++
    1. Static member functions belong to the class rather than to any specific object.
    2. They can be called using the class name followed by the scope resolution operator ::, without needing an object of the class.
    3. Static member functions cannot access non-static data members or member functions directly (unless they are also static), as they don't have access to any specific object's data.
    4. They are commonly used for utility functions or for operations that do not depend on specific object state.


    Syntex :
    class MyClass {
    public:
        static void staticMemberFunction() {
            // Function body
        }
    };
    


    Example:

    Lets implement a C++ program demonstrating the usage of static methods to increment a static variable without instantiating any objects. How does this approach differ from traditional object-oriented programming?

    #include<iostream>
    using namespace std;
    
    class Account {
        static int count; // static variable declaration
    public:
        static void display(); // static member function declaration
    };
    
    int Account::count = 56; // static data definition
    
    void Account::display() {
        count++;
        cout << "The value of count is: " << count << endl;
    }
    
    int main() {
        Account::display();
        Account::display();
    }
    
    Output:
    The value of count is: 57
    The value of count is: 58
    

    The display() function is declared as a static member function within the Account class.The count variable is declared as static within the class. The static variable count is initialized outside the class. In the main() function, the display() function is called using the scope resolution operator :: without creating objects of the Account class.



  3. Static Local Variables (within member functions):
    1. Static local variables declared within member functions retain their values between function calls.
    2. They are initialized only once, at the first call of the function containing them.
    3. They are accessible only within the scope of the function in which they are declared.
  4. class MyClass {
    public:
        void memberFunction() {
            static int staticLocalVariable = 0; // Static local variable
            staticLocalVariable++;
        }
    };
    


    Here's a simple program implementing the MyClass class with a static local variable inside the memberFunction():

    #include <iostream>
    
    using namespace std;
    
    class MyClass {
    public:
        void memberFunction() {
            static int staticLocalVariable = 0; // Static local variable
            staticLocalVariable++;
            cout << "Static local variable value: " << staticLocalVariable << endl;
        }
    };
    
    int main() {
        MyClass obj1, obj2;
    
        // Calling memberFunction on obj1
        obj1.memberFunction(); // Output will be 1
        obj1.memberFunction(); // Output will be 2
    
        // Calling memberFunction on obj2
        obj2.memberFunction(); // Output will be 3
    
        return 0;
    }
    
    Output:
    Static local variable value: 1
    Static local variable value: 2
    Static local variable value: 3
    

This program creates an instance of MyClass named obj1 and obj2 and calls the memberFunction() on each object. The static local variable staticLocalVariable retains its value between function calls for each object.