How to use and access static data member in C++ program?

All QuestionsCategory: Cpp LanguageHow to use and access static data member in C++ program?
Chetan Shidling Staff asked 5 years ago

I need short information.

2 Answers
Chetan Shidling Staff answered 5 years ago

Code:

#include <iostream>
using namespace std;
class Base
{
public:
int x;
static int y;
};
int Base::y;
int main()
{
Base b1;
b1.x = 10;
b1.y =30;
Base b2;
b2.x =20;
b2.y =40;
cout<<b1.x<<endl;
cout<<b1.y<<endl;
cout<<b2.x<<endl;
cout<<b2.y<<endl;
return 0;
}

Output:

10
40
20
40

chetan shidling answered 5 years ago

The above code will works properly or not? Is it correct example?