Usage and effect of a static data member : Static « Language « C++






Usage and effect of a static data member

Usage and effect of a static data member
#include <iostream>
using namespace std;
class shared {
  static int a;
  int b;
public:
  void set(int i, int j) {
    a=i; 
    
    b=j;
  }
  void show();
} ;
int shared::a;
void shared::show()
{
  cout << "This is static a: " << a;
  cout << "\nThis is non-static b: " << b;
  cout << "\n";
}
int main()
{
  shared x, y;
  x.set(1, 1); 
  x.show();
  y.set(2, 2); 
  y.show();
  x.show(); 
  return 0;
}


           
       








Related examples in the same category

1.Static function and static variableStatic function and static variable
2.Static function variableStatic function variable
3.Static Member Functions: its strictionsStatic Member Functions: its strictions
4.Static member functions: 'preinitialize' private static dataStatic member functions: 'preinitialize' private static data
5.A static member variable example.A static member variable example.
6.Use a static member variable independent of any object.Use a static member variable independent of any object.
7.Init static data before object creationInit static data before object creation
8.A shared resource example.A shared resource example.