Declaration of Static Method in C++
In C++, you write the method declaration inside the class, just like you would for any other function, to define a static method. But before the method name, you also use the static keyword. Here’s an illustration:
To call a static method, the following implementation is specified in the method body which is written outside of the class as with the other methods. Here’s an illustration:
We must use the class name, the scope resolution operator, and the method name to invoke a static method. Now, let’s understand how to call the static method function of the class.
It’s necessary to keep in mind that the static methods can only access the class’s static methods and static data members. Since they pertain to particular instances of the class, they are unable to access the non-static data members or methods.
Demonstration of Accessing the Static Function and Static Data Member
Here is the very first illustration of the static method in C++. First, we include the iostream header file and use the std namespace. This allows us to use the input and output operations such as cout to print the output to the console.
Next, we define the class of the static_class name before the main function. This class contains two members which are public members so that we can easily access outside the class. The first public member is the number of instances of the āstatic_classā class that are produced and returned by the static member function, numberOfInstances(). The second member, the instanceCount, which is the number of instances of the āstatic_classā class that are produced is tracked by a static data member named instanceCount.
It should be noted that the int static_class::instanceCount = 0; line outside the class declaration initializes the instanceCount data member to 0. The āstatic_classā class maintains the track of how many instances are generated, and the numberOfInstances() member function simply returns the value of the instanceCount data member.
using namespace std;
class static_class {
public:
static int numberOfInstances();
static int instanceCount;
};
int static_class::instanceCount = 0;
int static_class::numberOfInstances() {
return instanceCount;
}
int main() {
static_class a, b, c;
static_class::instanceCount += 3;
cout << "Number of instances created: " << static_class::numberOfInstances() << endl;
return 0;
}
Now, we start the main() function so that we can call the previously-defined functions and get the desired output. We create three instances of the āstatic_classā class named a, b, and c in the main() method. Using the static_class::instanceCount += 3, we then increase the instanceCount data member by the number of instances produced. The number of instances produced is then printed using the syntax cout “Number of instances created: ” static_class::numberOfInstances() endl. The output of this program is as follows:
Demonstration of Static Method in C++ which Gives the Factorial of Numbers
Here is another demonstration that we implement so that we can understand the working of static methods in C++ language. As we already know, in every C++ code, we first include the basic header files of C++ so that the program can execute and give us the desired output.
First, we define the class which is the Math class because we want to calculate the factorial this time. We create only one static method in our Math class, named factorial. This function returns the factorial of ānā given an integer input of ānā. We use a static keyword before the factorial() function. As a result, as we’ll see later, we may invoke the method using the scope resolution operator:: on the class name itself.
Then, we first determine whether the input ānā is negative within the factorial technique. If it is zero, we return -1 and output an error message to the standard error stream cerr. We then determine if the input ānā is equal to zero if it is not negative. The definition of n’s factorial is 1 if ānā is equal to 0. As a result, we return 1. The factorial of ānā is then calculated using a loop and returned if ānā is a positive integer.
#include <vector>
using namespace std;
class Math {
public:
static int factorial(int n);
};
int Math::factorial(int n) {
if (n < 0) {
cerr << "Error: Factorial of negative numbers is undefined." << endl;
return -1;
}
if (n == 0) {
return 1;
}
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
vector<int> numbers = {0, 1, 2, 3, 4, 5};
for (int n : numbers) {
cout << "Factorial of " << n << " is " << Math::factorial(n) << endl;
}
return 0;
}
Then, we call the main() function. In the main() function, we generate a vector of integer numbers with the values of 0 through 5. Then, using the static function Math::factorial, we loop over each integer ānā in the numbers vector, get its factorial, and publish the result to the standard output stream cout. The following is the output:
Conclusion
We learned what the static method in C++ programming language is and why we are using it in the C++ language. We learned how to define the static method in C++ in detail and how we access the method and its member in the program. We also illustrated some examples of static methods so that we can easily understand the working of it in C++.