標(biāo)題: C++語言靜態(tài)數(shù)據(jù)成員與靜態(tài)成員函數(shù) [打印本頁]

作者: daming    時間: 2014-12-30 02:00
標(biāo)題: C++語言靜態(tài)數(shù)據(jù)成員與靜態(tài)成員函數(shù)
本帖最后由 daming 于 2014-12-30 02:15 編輯


  1. #include<iostream>
  2. using namespace std;
  3. class Cat
  4. {
  5. private:
  6. static int HowManyCats;
  7. public:
  8. Cat();
  9. ~Cat();
  10. static int GetHowMany();
  11. };

  12. int Cat::HowManyCats=0;

  13. Cat::Cat()
  14. {
  15. HowManyCats++;
  16. }

  17. Cat::~Cat()
  18. {
  19. HowManyCats--;
  20. }

  21. int Cat::GetHowMany()
  22. {
  23. return HowManyCats;
  24. }

  25. void main()
  26. {
  27. Cat a1,a2,a3;
  28. cout<<Cat::GetHowMany()<<endl;
  29. {
  30.   Cat a4,a5,a6;
  31.   cout<<a4.GetHowMany()<<endl;
  32. }
  33. cout<<a1.GetHowMany()<<endl;
  34. }
復(fù)制代碼







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1