×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Static Variables and Functions | Find output programs | Set 2

This section contains the C++ find output programs with their explanations on C++ Static Variables and Functions (set 2).
Submitted by Nidhi, on June 25, 2020

Program 1:

#include <iostream>
using namespace std;

int* fun()
{
    static int a;
    a++;

    return &a;
}

int main()
{
    int** p;

    p = &(fun());

    fun();
    fun();
    fun();

    cout << "function called " << **p << " times";

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:16:16: error: lvalue required as unary ‘&’ operand
     p = &(fun());
                ^

Explanation:

This code will generate an error because of the below statement,

p = &(fun());

In the above statement p is a chain pointer, but we are trying to get the address of address that does not exist.

Program 2:

#include <iostream>
using namespace std;

int* fun()
{
    static int a;
    a++;

    return &a;
}
static int a = 10;

int main()
{
    int* p;

    p = fun();

    fun();
    fun();
    fun();

    cout << "function called " << *p << " times";

    return 0;
}

Output:

function called 4 times

Explanation:

Here, we created a function that contains a static variable a and function returns an integer pointer.

And one more global static variable a is created and initialize with 10, here global and local both a are different variables.

Now, coming to the main() function, here we created an integer pointer p that is storing the address returned by function fun(), after every function call, the static variable gets increased, because the default value of the static variable is 0 and then its lifetime is in the whole program.

So, finally it will print "function called 4 times" on the console screen. After calling function fun() 4 times.

Program 3:

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    Sample()
    {
        A++;
    }
    void display()
    {
        cout << A << endl;
    }
};

int main()
{
    Sample S;
    S.display();
    return 0;
}

Output:

/tmp/ccjOw4Qx.o: In function `Sample::Sample()':
main.cpp:(.text._ZN6SampleC2Ev[_ZN6SampleC5Ev]+0xa): undefined reference to `Sample::A'
main.cpp:(.text._ZN6SampleC2Ev[_ZN6SampleC5Ev]+0x13): undefined reference to `Sample::A'
/tmp/ccjOw4Qx.o: In function `Sample::display()':
main.cpp:(.text._ZN6Sample7displayEv[_ZN6Sample7displayEv]+0xe): undefined reference to `Sample::A'
collect2: error: ld returned 1 exit status

Explanation:

This code will generate a syntax error (Undefined reference to 'Sample::A') because we declared a static variable, but we did not define it properly.

The below program will demonstrate how to define static members of the class?

#include <iostream>
using namespace std;

class Sample {
    static int A;

public:
    Sample()
    {
        A++;
    }
    void display()
    {
        cout << A << endl;
    }
};
int Sample::A = 0;

int main()
{
    Sample S;
    S.display();
    return 0;
}

Output:

1




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.