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


Warning: Trying to access array offset on false in /home3/indiakep/public_html/wp-content/plugins/dw-question-answer/inc/Template.php on line 8
All QuestionsCategory: Cpp LanguageHow to use and access static data member in C++ program?
Chetan Shidling Staff asked 6 years ago

I need short information.

2 Answers
Chetan Shidling Staff answered 6 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 6 years ago

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