The static keyword is a reserved word in the C and C++ programming languages. It is employed to declare static data members or static member functions within or outside a class. To grasp the concept of static data members and static member functions better, let's examine some example programs.
Static data member
When a class's data member is declared with the static keyword, it becomes a static data member. Just like static member functions, static data members can only be accessed through static member functions or static data members. Additionally, all instances of the class share a single instance of the static member to interact with the static data.
Syntax
static data_type data_member;
Here, the static keyword is a reserved term in the standard library.
The data_type represents the type of variable in C++, like int, float, string, and more.
The data_member is the name of the static data.
Example 1: We will now develop a basic application to retrieve the static properties in the C++ programming language.
#include <iostream>
#include <string.h>
using namespace std;
// create class of the Car
class Car
{
private:
int car_id;
char car_name[20];
int marks;
public:
// declare a static data member
static int static_member;
Car()
{
static_member++;
}
void inp()
{
cout << " \n\n Enter the Id of the Car: " << endl;
cin >> car_id; // input the id
cout << " Enter the name of the Car: " << endl;
cin >> car_name;
cout << " Number of the Marks (1 - 10): " << endl;
cin >> marks;
}
// display the entered details
void disp ()
{
cout << " \n Id of the Car: " << car_id;
cout << "\n Name of the Car: " << car_name;
cout << " \n Marks: " << marks;
}
};
// initialized the static data member to 0
int Car::static_member = 0;
int main ()
{
// create object for the class Car
Car c1;
// call inp() function to insert values
c1. inp ();
c1. disp();
//create another object
Car c2;
// call inp() function to insert values
c2. inp ();
c2. disp();
cout << " \n No. of objects created in the class: " << Car :: static_member <<endl;
return 0;
}
Output
Enter the Id of the Car:
101
Enter the name of the Car:
Ferrari
Number of the Marks (1 - 10):
10
Id of the Car: 101
Name of the Car: Ferrari
Marks: 10
Enter the Id of the Car:
205
Enter the name of the Car:
Mercedes
Number of the Marks (1 - 10):
9
Id of the Car: 205
Name of the Car: Mercedes
Marks: 9
No. of objects created in the class: 2
Static Member Functions
Static member functions are unique functions designed for accessing static data members or other static member functions. A function is declared as a member using the keyword static. A static member function provides a single instance shared among all objects of the class. Access to a static member function is achieved by referencing the class name or the class' objects. Should a static member function try to access a non-static data member or function, an error will be generated.
Syntax
class_name::function_name (parameter);
Here, the class_name is the name of the class.
The function_name refers to the identifier of the static member function.
parameter: It specifies the identifier for passing arguments to the static member function.
Let's develop a new program to call the static member function by utilizing the class name in the C++ programming language.
#include <iostream>
using namespace std;
class Note
{
// declare a static data member
static int num;
public:
// create static member function
static int func ()
{
return num;
}
};
// initialize the static data member using the class name and the scope resolution operator
int Note :: num = 5;
int main ()
{
// access static member function using the class name and the scope resolution
cout << " The value of the num is: " << Note:: func () << endl;
return 0;
}
Output
The value of the num is: 5
Let's develop an additional program to invoke the static member function by utilizing the object of the class in the C++ programming language.
#include <iostream>
using namespace std;
class Note
{
// declare a static data member
static int num;
public:
// create static member function
static int func ()
{
cout << " The value of the num is: " << num << endl;
}
};
// initialize the static data member using the class name and the scope resolution operator
int Note :: num = 15;
int main ()
{
// create an object of the class Note
Note n;
// access static member function using the object
n.func();
return 0;
}
Output
The value of the num is: 15
Example 4: Let's explore a scenario where we access a static member function by utilizing both the object and class within the C++ programming language.
#include <iostream>
using namespace std;
class Member
{
private:
// declaration of the static data members
static int A;
static int B;
static int C;
// declare public access specifier
public:
// define the static member function
static void disp ()
{
cout << " The value of the A is: " << A << endl;
cout << " The value of the B is: " << B << endl;
cout << " The value of the C is: " << C << endl;
}
};
// initialization of the static data members
int Member :: A = 20;
int Member :: B = 30;
int Member :: C = 40;
int main ()
{
// create object of the class Member
Member mb;
// access the static member function using the class object name
cout << " Print the static member through object name: " << endl;
mb. disp();
// access the static member function using the class name
cout << " Print the static member through the class name: " << endl;
Member::disp();
return 0;
}
Output
Print the static member through object name:
The value of the A is: 20
The value of the B is: 30
The value of the C is: 40
Print the static member through the class name:
The value of the A is: 20
The value of the B is: 30
The value of the C is: 40