Static Data Member Static Member Function
Static Data Member Static Member Function
Output
Static member function called
The value of a : 8
The static variable value : 28
#include <iostream>
using namespace std;
class Test
{
static int x; Answer: (C)
public:
Test() { x++; } Explanation: Static functions can be called
static int getX() {return x;}
without any object. So the call “Test::getX()” is
};
fine.
int Test::x = 0;
Since x is initialized as 0, the first call to getX()
int main() returns 0. Note the statement x++ in
{ constructor. When an array of 5 objects is
cout << Test::getX() << " "; created, the constructor is called 5 times. So x
Test t[5]; is incremented to 5 before the next call to
cout << Test::getX();
}
getX().
(A) 0 0
(B) 5 5
(C) 0 5
(D) Compiler Error