What will be printed out as a result of the following code execution?

#include <iostream>
class A {
public:
    int k;
    void SetCount() {
        static int n = 0;
        k = n++;
    };
};

int main() {
    A *pA = new A();
    pA->SetCount();
    std::cout<<pA->k;
    delete pA;

    pA = new A();
    pA->SetCount();
    std::cout<<pA->k;

    A a;
    a.SetCount();
    std::cout<<a.k;
    delete pA;

    return 0;
}
Explanation
Despite the fact that objects and pointers to objects of class A destroyed in main() static members still exists and could be accessed and changed

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes