ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2111Static Members Functio…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2111

    Object Oriented Programming

    Progress0 / 23 done
    Topics23
    1Introduction to object oriented design2History and advantages of object oriented design3Introduction to object oriented programming concepts4Classes and objects5Data encapsulation6Constructors and destructors7Access modifiers8Const vs non-const functions9Static data members & functions10Function overloading11Operator overloading12Identification of classes and their relationships13Composition and aggregation14Inheritance15Multiple inheritance16Polymorphism17Abstract classes and interfaces18Generic programming concepts19Function & class templates20Standard template library21Object streams22Data and object serialization using object streams23Exception handling
    4 min read
    Topic 9 of 23
    Topic Study Note Illustration
    09

    Static data members & functions

    Static Data Members and Functions in C++

    Static Data Members and Functions in C++

    In C++, static is a keyword that can be used with both data members and member functions in a class. When you declare a data member or a function as static, it behaves differently than regular members. Static members are shared among all instances (objects) of the class, rather than each object having its own copy.


    Static Data Members

    A static data member is a class variable that is shared by all objects of that class. Unlike regular data members, which are unique to each object, a static data member has only one copy for all instances of the class.

    Key Characteristics of Static Data Members:

    1. Shared by all objects: All objects of the class share the same value for the static data member.
    2. Memory allocation: Memory is allocated only once for the static data member, regardless of how many objects of the class are created.
    3. Access: A static data member can be accessed using the class name without creating an object.

    Syntax:

    class ClassName {
    public:
        static dataType variableName;  // Declare static data member
    };
    

    Example:

    class Counter {
    public:
        static int count;  // Static data member
        Counter() {
            count++;  // Increment count whenever a new object is created
        }
    };
    
    // Define the static data member outside the class
    int Counter::count = 0;
    
    int main() {
        Counter c1;
        Counter c2;
        cout << "Count: " << Counter::count << endl;  // Access via class name
        return 0;
    }
    

    In this example, count is a static data member. Each time a Counter object is created, the count variable is incremented, and all Counter objects share the same count value. The static data member is initialized outside the class with int Counter::count = 0;.


    Static Member Functions

    A static member function is a function that is associated with the class itself, rather than with any particular object. This means that it can be called without creating an object of the class. Static member functions can only access static data members and other static functions. They cannot access non-static members because non-static members belong to specific objects.

    Key Characteristics of Static Member Functions:

    1. Can be called without an object: You can call static functions using the class name.
    2. Can access only static members: They cannot access non-static data members or non-static member functions.
    3. Shared across all objects: Just like static data members, static functions are shared across all objects of the class.

    Syntax:

    class ClassName {
    public:
        static returnType functionName() {
            // Function code
        }
    };
    

    Example:

    class Counter {
    public:
        static int count;
        Counter() {
            count++;
        }
    
        static int getCount() {
            return count;  // Can access static data member
        }
    };
    
    // Define the static data member outside the class
    int Counter::count = 0;
    
    int main() {
        Counter c1;
        Counter c2;
        cout << "Count: " << Counter::getCount() << endl;  // Calling static function
        return 0;
    }
    

    In this example, getCount() is a static member function. It is called using the class name (Counter::getCount()), and it can access the static data member count directly.


    Key Differences Between Static and Non-Static Members:

    Feature Static Data/Function Non-Static Data/Function
    Memory Allocation Shared by all objects Each object gets its own copy
    Access Can be accessed with class name (without an object) Must be accessed with an object
    Association Associated with the class Associated with a specific object
    Access to Non-Static Members Cannot access non-static members Can access both static and non-static members

    Why Use Static Members?

    1. Shared Data: Static data members are useful for variables that should be shared among all instances of a class, such as a counter for the number of objects created.
    2. Utility Functions: Static functions can be used for utility functions that are related to the class but don’t need to operate on individual object data.
    3. Memory Efficiency: Static members are not duplicated for each object, saving memory when many objects of a class are created.

    Using static members properly can make your class design more efficient and clear, especially when managing shared resources or functions that are not dependent on individual object states.

    ← Previous topic 8
    Const vs non-const functions
    Next topic 10 →
    Function overloading

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    707words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming