In this lesson, we’ll learn two important things:

  • The concept of static members in classes that provide class-level functionality and shared data among instances.

  • The practice of separating the declaration and definition of a class into different files. This coding practice offers several benefits, including enhanced modularity, reusability, and maintainability. By adopting this approach, we can create more organized and scalable classes that are easier to work with and improve overall compilation times.

Static member variables and member functions

Static data members are another essential aspect that gives the OOP the versatility of class manipulation. These special members are in C++ and are associated with the class rather than individual objects. Before we jump to the example, let’s see what the static members are.

Static member variable

In C++, static member variables are class variables that are shared among all objects of the class. They are declared using the static keyword within the class definition and must be defined and initialized outside the class scope. Static member variables provide a way to maintain and share common data among class instances. A static variable is not the property of any object but actually the property of the class. It’s stored in the global/static section of the memory. It keeps residing there until the end of the program. The static attribute is accessible anywhere in the scope of the class.

Let’s run an example to see how this works:

Get hands-on with 1200+ tech skills courses.