c++——静态成员静态函数

柔光的暖阳◎ 2023-08-17 15:45 232阅读 0赞
  1. #include<iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. public:
  6. static int objectCount;
  7. Box(double l=2.0,double b=2.0,double h = 2.0)
  8. {
  9. cout << "constructor called." << endl;
  10. length = l;
  11. breadth = b;
  12. height = h;
  13. objectCount++;
  14. }
  15. double Volume()
  16. {
  17. return length * breadth*height;
  18. }
  19. static int getCount()
  20. {
  21. return objectCount;
  22. }
  23. private:
  24. double length;
  25. double breadth;
  26. double height;
  27. };
  28. int Box::objectCount = 0;
  29. int main()
  30. {
  31. cout << "initial stage count:" << Box::getCount() << endl;
  32. Box Box1(2.9,2.0,3.4);
  33. Box Box2(2.2, 2.0, 3.4);
  34. Box Box3(2.2, 2.0, 3.4);
  35. cout << "Total Objects:" << Box::objectCount << endl;
  36. return 0;
  37. }

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++——静态成员函数

    一 静态成员函数概念: 作用:在声明对象之前访问私有静态数据成员; 静态成员函数与类相关联。 静态成员函数只能访问静态成员(静态成员变量和静态成员函数); 若要在静态成

    相关 C++静态成员函数

    一 点睛 与静态数据成员不同,静态成员函数的作用不是为了对象之间的沟通,而是为了能处理静态数据成员。 静态成员函数没有this指针。既然它没有指向某一对象,也就无法对一个对