HomeLinuxStatic Strategies in C++

Static Strategies in C++


A static methodology within the C++ programming language is a technique that’s linked to a category as an alternative of being a selected occasion that’s related to that class. It’s a approach which may be used with out developing a category occasion, giving it a straightforward technique to ship the class-related performance with out creating objects. In C++, a static methodology is asserted with the static key phrase. When a way is marked as static, it’s linked to the category as a complete fairly than a particular occasion. This means that as an alternative of using an object of the category, the approach could also be invoked by using the category identify which is adopted by the operator for scope decision (::).

Declaration of Static Technique in C++

In C++, you write the strategy declaration inside the category, identical to you’d for some other perform, to outline a static methodology. However earlier than the strategy identify, you additionally use the static key phrase. Right here’s an illustration:

To name a static methodology, the next implementation is specified within the methodology physique which is written exterior of the category as with the opposite strategies. Right here’s an illustration:

We should use the category identify, the scope decision operator, and the strategy identify to invoke a static methodology. Now, let’s perceive name the static methodology perform of the category.

It’s essential to remember the fact that the static strategies can solely entry the category’s static strategies and static knowledge members. Since they pertain to explicit situations of the category, they’re unable to entry the non-static knowledge members or strategies.

Demonstration of Accessing the Static Perform and Static Information Member

Right here is the very first illustration of the static methodology in C++. First, we embody the iostream header file and use the std namespace. This enables us to make use of the enter and output operations comparable to cout to print the output to the console.

Subsequent, we outline the category of the static_class identify earlier than the principle perform. This class comprises two members that are public members in order that we will simply entry exterior the category. The primary public member is the variety of situations of the “static_class” class which are produced and returned by the static member perform, numberOfInstances(). The second member, the instanceCount, which is the variety of situations of the “static_class” class which are produced is tracked by a static knowledge member named instanceCount.

It needs to be famous that the int static_class::instanceCount = 0; line exterior the category declaration initializes the instanceCount knowledge member to 0. The “static_class” class maintains the monitor of what number of situations are generated, and the numberOfInstances() member perform merely returns the worth of the instanceCount knowledge member.

#embody <iostream>
utilizing namespace std;
class static_class {
public:
    static int numberOfInstances();
    static int instanceCount;
};
int static_class::instanceCount = 0;
int static_class::numberOfInstances() {
    return instanceCount;
}
int principal() {
    static_class a, b, c;
    static_class::instanceCount += 3;
    cout << “Variety of situations created: “ << static_class::numberOfInstances() << endl;
    return 0;
}

Now, we begin the principle() perform in order that we will name the previously-defined capabilities and get the specified output. We create three situations of the “static_class” class named a, b, and c in the principle() methodology. Utilizing the static_class::instanceCount += 3, we then improve the instanceCount knowledge member by the variety of situations produced. The variety of situations produced is then printed utilizing the syntax cout “Variety of situations created: ” static_class::numberOfInstances() endl. The output of this program is as follows:

Demonstration of Static Technique in C++ which Provides the Factorial of Numbers

Right here is one other demonstration that we implement in order that we will perceive the working of static strategies in C++ language. As we already know, in each C++ code, we first embody the essential header information of C++ in order that this system can execute and provides us the specified output.

First, we outline the category which is the Math class as a result of we wish to calculate the factorial this time. We create just one static methodology in our Math class, named factorial. This perform returns the factorial of “n” given an integer enter of “n”. We use a static key phrase earlier than the factorial() perform. Because of this, as we’ll see later, we might invoke the strategy utilizing the scope decision operator:: on the category identify itself.

Then, we first decide whether or not the enter “n” is damaging throughout the factorial approach. Whether it is zero, we return -1 and output an error message to the usual error stream cerr. We then decide if the enter “n” is the same as zero if it’s not damaging. The definition of n’s factorial is 1 if “n” is the same as 0. Because of this, we return 1. The factorial of “n” is then calculated utilizing a loop and returned if “n” is a constructive integer.

#embody <iostream>
#embody <vector>
utilizing namespace std;
class Math {
public:
    static int factorial(int n);
};
int Math::factorial(int n) {
    if (n < 0) {
        cerr << “Error: Factorial of damaging numbers is undefined.” << endl;
        return 1;
    }
    if (n == 0) {
        return 1;
    }
    int outcome = 1;
    for (int i = 1; i <= n; i++) {
        outcome *= i;
    }
    return outcome;
}

int principal() {
    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 name the principle() perform. In the principle() perform, we generate a vector of integer numbers with the values of 0 via 5. Then, utilizing the static perform Math::factorial, we loop over every integer “n” within the numbers vector, get its factorial, and publish the outcome to the usual output stream cout. The next is the output:

Conclusion

We realized what the static methodology in C++ programming language is and why we’re utilizing it within the C++ language. We realized outline the static methodology in C++ intimately and the way we entry the strategy and its member in this system. We additionally illustrated some examples of static strategies in order that we will simply perceive the working of it in C++.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments