tampurus

9 Static data member and member function

Dec 2nd, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class BCA{
  4.     int x;
  5.     static int count;
  6.     public:
  7.     // defualt constructor
  8.     BCA(){
  9.         count++;
  10.     }
  11.     static int getcount(){
  12.         return count;
  13.     }
  14. };
  15. int BCA::count=0;
  16. int main()
  17. {
  18.     cout<<"Intial value of count. "<<BCA::getcount()<<endl;
  19.     BCA a;
  20.     cout<<"count after 1st obj. "<<BCA::getcount()<<endl;
  21.     BCA b;
  22.     cout<<"count after 2nd obj. "<<BCA::getcount()<<endl;
  23.     BCA c;
  24.     cout<<"count after 3rd obj. "<<BCA::getcount()<<endl;
  25.     return 0;
  26. }
  27. /*
  28. Output
  29. Intial value of count. 0
  30. count after 1st obj. 1
  31. count after 2nd obj. 2
  32. count after 3rd obj. 3
  33. */
Advertisement
Add Comment
Please, Sign In to add comment