Pages

Sunday, May 11, 2025

Static Keyword - Static Variable in OOPs (02)

 #include <iostream>

using namespace std;

class Example {
public:
    static int x; // remember to do it const, only way makes it work

};

int Example::x = 0;  // now main func, will share this same object of x.

int main() {
    Example eg1;
    Example eg2;
    Example eg3;
   
    cout << eg1.x++ << endl; // 0
    cout << eg2.x++ << endl; // 1
    cout << eg3.x++ << endl; // 2


    return 0;
}

No comments:

Post a Comment

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...