ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    COMP2111
    Progress0 / 23 topics
    Topics
    1. Introduction to object oriented design2. History and advantages of object oriented design3. Introduction to object oriented programming concepts4. Classes and objects5. Data encapsulation6. Constructors and destructors7. Access modifiers8. Const vs non-const functions9. Static data members & functions10. Function overloading11. Operator overloading12. Identification of classes and their relationships13. Composition and aggregation14. Inheritance15. Multiple inheritance16. Polymorphism17. Abstract classes and interfaces18. Generic programming concepts19. Function & class templates20. Standard template library21. Object streams22. Data and object serialization using object streams23. Exception handling
    COMP2111›Static data members & functions
    Object Oriented ProgrammingTopic 9 of 23

    Static data members & functions

    4 minread
    707words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count707
      Code examples0
      DifficultyBeginner