C++

Static Method C++

A method in C++ is also known as a function, and using methods in C++ promotes the concept of modular programming and code reusability. It means the methods that are once written can be called repetitively for as many times as needed without having the necessity of writing them every time. These methods have the following three basic properties:

  • The static methods can be retrieved directly with the class name and the scope resolution operator without creating any object.
  • The static methods of a class can only access the static members of that class.
  • The static methods cannot access the non-static members of a class.

We have designed this article to teach you the usage of the static methods in C++ in Ubuntu 20.04.

Using the Static Method in C++ in Ubuntu 20.04

To use the static methods in C++ in Ubuntu 20.04, you first need to go through all the examples provided below to have a good idea of how these functions work in C++.

Example # 1: Exploring the First Property of the Static Methods in C++

In this example, we wish to explore the first property of the static methods in C++; the static methods of a class can be accessed directly with the class name while using the scope resolution operator. For that, we have written a C++ script shown in the following image:

Static Method C++

In this C++ script, we have defined a class named “Number”. Inside the body of this class, we only have one public function. We have declared this function as “static”. The name of this function is “PrintNum”, and it takes the number “n” as its only parameter. Within this function, we simply want to print out the value of this passed number on the terminal. As you can see, we have not defined any constructor for this class. This means that we do not intend to create its object. Instead, we are going to access the functions of this class directly.

Now, within our “main()” function, we have accessed the “PrintNum” function of the “Number” class with the help of the class name and the scope resolution operator. While calling this function, we have passed it a random number, i.e., 25. Our “main()” function wraps up with the “return 0” statement since we declared it to have an integer return type.

When we compiled and executed this C++ script, our number was correctly printed on the terminal, as shown in the image below. It means that the first property of the static methods in C++ has been satisfied — the static methods can be accessed directly with the class name without creating any object, and they work exactly as intended.

Static Method in c++

Example # 2: Exploring the Second Property of the Static Methods in C++

In this example, we want to explore the second property of the static methods in C++; the static methods of a class can only access the static members of that class. For that, we have written a C++ script shown in the following image:

In this C++ script, we have first defined a class named “Number”. Inside the body of this class, we have a private member “x” that is of integer data type, and we have made it static. Then, we only have one public function. We have declared this function as “static”. The name of this function is “PrintNum”, and it takes the number “n” as its only parameter. Within this function, we want to print out the value of this passed number on the terminal and the value of the static member “x”.

After that, we have initialized the static member “x” with a value “10” with the help of the class name outside our class without using the “static” keyword again. Now, within our “main()” function, we have accessed the “PrintNum” function of the “Number” class with the help of the class name and the scope resolution operator. While calling this function, we passed it a random number, i.e., 25. Our “main()” function wraps up with the “return 0” statement since we declared it to have an integer return type.

When we compiled and executed this C++ script, our number, as well as the value of the variable “x”, was correctly printed on the terminal, as shown in the image below. It means that the second property of the static methods in C++ has been satisfied — the static methods can only access the static members of a class in C++.

Example # 3: Exploring the Third Property of the Static Methods in C++

In this example, we wish to explore the third property of the static methods in C++, which is, in fact, the other way of stating the second property; the static methods cannot access the non-static members of a class. For that, we have written a C++ script shown in the following image:

This C++ script looks exactly like the script shown in the second example. However, the only difference is that this time, we have not declared the variable “x” as static.

When we compiled and executed this C++ script, an error message was generated on the terminal as shown in the image below, stating something like the value of “x” cannot be accessed by a static method in C++. It means that the third property of the static methods in C++ has been satisfied — the static methods cannot access any non-static members of a class in C++.

Example # 4: Generating Consecutive Roll Numbers by Using the Static Methods in C++

In this example, we just wanted to give an overall view of how the static methods work in C++ by wrapping up our examples. We will just be creating a program to generate some roll numbers within the provided range. For that, we have written a C++ script shown in the following image:

In this C++ script, we have a class named “RollNumber”. Within this class, we have a private static member “RollNum” of integer data type. Then, we have a public static method “getRollNum()” with the integer return type. Outside the definition of this class, we have initialized our “RollNum” variable with a value “1” and defined our “getRollNum()” function too to return the incremented “RollNum” every time it is being called.

Then, within our “main()” function, we have a “for” loop that iterates through a counter variable from “0” to “9”, which is for 10 iterations. Inside this loop, we want to print the value returned by the “getRollNum()” function for every iteration. Again, the “main()” function wraps up with the “return 0” statement.

When we compiled and executed this C++ script, a series of 10 different roll numbers was generated on the terminal as shown in the following image:

Conclusion

Our goal for this article was to teach you the usage of the static methods in C++ in Ubuntu 20.04. We shared the basic properties of these methods, followed by the four examples through which you can instantly learn how these methods work in C++. After understanding these examples, you can easily acquire a good command over the static methods in C++. We hope you found this article helpful, and check out Linux Hint for more informative articles.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.