Static function and static variable in cpp

Static variable and Static method in cpp

PROGRAM:

  1. #include<iostream>
  2. using namespace std;
  3. class test{
  4. int code;
  5. static int Count;
  6. public:
  7. void set_code(void){code=++Count;}
  8. void show_code(void){
  9. cout<<"Object number : "<<code<<endl;
  10. }
  11. static void show_Count(void){
  12. cout<<"Count : "<<Count<<endl;
  13. }
  14. };
  15.  
  16. int test:: Count;
  17.  
  18. int main(){
  19. test t1,t2; //!when 1st obj. created static Count=0
  20. t1.set_code();//!t1=1,count=1
  21. test::show_Count();//!because Static function
  22. t2.set_code(); //!t2=2,count=2
  23. test::show_Count();//!count=2
  24. test t3;//!count=2
  25. t3.set_code();//!t3=3,count=3
  26. test::show_Count();//!count=3
  27.  
  28. t1.show_code();
  29. t2.show_code();
  30. t3.show_code();
  31.  
  32. return 0;
  33. }
  34.  

Comments

Popular posts from this blog

Introduction to ChatGPT: Explain what ChatGPT is, how it works, and what it is used for.

Multi Level Inheritance In cpp

Hybrid Inheritance In cpp