I am studying for my finals and there is a topic which is bugging me recently i don't understand why we use static variables or static data members in our code. Can anyone explain it to me how and why we use the static
keyword in C++
I have tried looking it up on different sites and i have tried some code, what i don't understand is why am i getting such results.
class myclass {
public:
int a,b;
inline int getVal();
};
inline int myclass :: getVal()
{
cout<<"Enter the value of a and b\n";
static int a = 9; //static keyword used.
cin>>a>>b;
}
int main()
{
myclass o1;
o1.getVal();
cout<<"\nThe value of a is : "<<o1.a<<"\nThe value of b is : "<<o1.b;
}
the value i am getting for a is 3, no matter what i enter? can anyone explain to me why is that?