Static Member in C++
Learn via video course

Overview
Member objects of a class are the class member variables that are objects of another class. If a class has an object of another class as a member variable, that binds them in the Has-A relation. A class can have a static member, that can be declared by using the static keyword. Static members of a class have a single copy for all objects of a class, can only be visible in class but have a lifeline until the program runs.
Member Objects
A class is a user-defined data type or data structure, which consists of member variables and member functions. To use the properties of the class or to access the member variables and member functions of the class, most of the time we have to create an object of the class.
Member variables in the class are nothing but just the data types which can be both primitive or user-defined. As the class is a user-defined data type that means an object of an already defined class can be a member variable of another class which is known as a Member object.
Example:
Output:
In the above example, we have created two classes first and second. In the second class, we defined a member variable which is an object of first class. Then in the main function accessed the member variable of first class, using the object of second class and its member object that is obj_first.
Constructor Calling
To initialize the member variables of a class instance or class object there are special member functions known as constructors. According to requirements (which member variable we want to initialize), we can define our own constructors in a class. Now, if we have a class that consists of a member object, to initialize the object of this class we will call the constructor that leads to multiple constructor calls. Initially, the constructors of all member objects are called after that program calls the constructor of the parent class which consists of all member objects. First, all member objects will be created and initialized then our complete object will be created and initialized. We will see this with an example in the next section.
Initialization of Member Objects
As we have discussed the constructors in the above section, we can conclude that the initialization of an object of any class totally depends upon the constructor.
Let’s take an example to get an idea about initialization.
We will create two classes:
- First Class: First class contains a member variable S with the data type string, a default constructor, a parameterized constructor, and a copy constructor.
- Second Class: This class contains three member variables, the first one is an integer variable, second and third both are member objects as they are objects of the first class. Further, there are two constructors, one is default and another one is parameterized. At last, there is an initializer list used to initialize all the class data members at the same time without assigning them values.
Output:
In the above example, we have taken three cases of initialization of a member object and object of a class that has a member object.
Case1(obj1) : By using default constructors, created and initialized both object and member object.
Case2 (obj2) : By using the default constructor for member object and parameterized constructor, we created and initialized object of the class.
Case3 (obj3) : Used initializer list to initialize both member objects and data object of our second class. In the above two cases, member objects first call their default constructor for initialization but in this case, the member object will directly get the value given in the initializer list and skip the default constructor call.
The above example provides three cases, but there could be many more, totally depending on the constructors defined by the user.
Constant Member Objects
When we need some constant values which we are surely not going to change in the future, we can declare them as constant values. Constant member objects are similar to other constant member variables. These are declared using the const keyword. Let’s look into its syntax first:
Syntax :
While declaring any variable or object as a constant using const keyword we must initialize it. So, if we are going to declare any member object as constant then we have to be careful about the choice of the constructor, as every constant member variable of the class must be initialized.
Has-A Relationship
There are two ways by which a class can use the properties of another class or we can say, there are two types of relations between classes.
- Is-A relation: When a class uses the properties of another class by means of inheritance, then the relation between them is Is-A relation.
- Has-A relation: When a class uses the properties of another class by having its object as a member variable, then the relation between them is Has-A relation.
The main motive of both the relations is to re-use the properties to avoid re-declaration.
Static Members
Imagine we have a class which stores information about the students of a school. In this class, there is a string variable that stores the name of the principal of the school. Now, as for every student variable, principal name will be the same. So, when we create a new object for this class, a new memory block for the principal name will be created. This will lead to wastage of memory. The solution to the above-defined problem is static data members.
Static members are defined using the static keyword. Static members of a class are allocated memory only once, and all objects belonging to that class can access them. Static members have a lifetime that spans the entire duration of the program's execution.
Static Data Members
Static data members are declared by using the static keyword inside the class, but as they have the lifetime until the program runs and is accessible to every class object they must be initialized outside the class. We initialize static data members after the class declaration and before the main function. The scope resolution operator is used to access them.
The memory block of static data members is shared by every object of the class. So, if there is any change made in the static data member, it will be updated for every other object of the class.
Static data members can be accessed anywhere in the program after the declaration of class either using the class instance or using scope resolution, class name, and variable name. There is no need to create objects to access them.
Let’s take an example to see the syntax and to get an idea about initialization:
Output :
In the above code, we have created a class which consists of a static data member var. Then, we have initialized var after the declaration of class and before the main function. Take a look over the syntax for initialization of static data members:
Syntax:
In the main function, we created two objects of class myClass. We made the changes in the value of the var by one object and showed that by another object.
Static Member Functions
As we have discussed about the static data members, similarly we have static member functions which are defined using the static keyword. Since, we have defined these functions as static, so they consists of class properties rather than object properties. We can access static member functions by using the class name and scope resolution operator.
Syntax :
As we can call static member function by just using class name and function name, they can be called even if no object of class be created.
A static member function can only access static data members, other static member functions, and any other functions from outside the class. Static member functions do not have access to this pointer because they have class scope.
Let’s see the example of static data members to get a better understanding of it:
Output :
In the above code, we have created a class that consists of static data and function members. Then we have initialized the static data member before the main function and in the main function using the static member function printed the value of var without even creating the object of our class myClass.
Conclusion
- Member objects of a class are the class member variables that are objects of another class that is already defined.
- When a class has a member object, the constructors of the member objects are called before the constructor of the main class when creating an object. This ensures that the member objects are initialized first.
- If a class has an object of another class as a member variable that binds them in the Has-A relation.
- Static members are defined using the static keyword. For static members only once the memory is allocated.
- Static data members are declared inside the class but they are initialized outside of the class.
- Static member functions can be accessed using class name and scope resolution operator. Also, we can call the member function without creating any object of the class.