CS Electrical And Electronics
@cselectricalandelectronics

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?
CS Electrical And Electronics Staff asked 4 years ago

I need short information.

2 Answers
CS Electrical And Electronics Staff answered 4 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 4 years ago

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