当前位置:   article > 正文

C++ day2

C++ day2

思维导图

封装一个矩形类

  1. #include <iostream>
  2. using namespace std;
  3. class Rect
  4. {
  5. private:
  6. int height;
  7. int width;
  8. public:
  9. void init(int w,int h)
  10. {
  11. width = w;
  12. height = h;
  13. }
  14. void set_w(int w)
  15. {
  16. width = w;
  17. }
  18. void set_h(int h)
  19. {
  20. height = h;
  21. }
  22. void show()
  23. {
  24. cout << "周长为:" << (width+height)*2 << endl;
  25. cout << "面积为:" << width * height << endl;
  26. }
  27. };
  28. int main()
  29. {
  30. Rect s1;
  31. s1.init(10,5);
  32. s1.show();
  33. int w,h;
  34. cout << "请输入更改的宽度:";
  35. cin >> w ;
  36. s1.set_w(w);
  37. cout << "请输入要更改的长度:";
  38. cin >> h;
  39. s1.set_h(h);
  40. s1.show();
  41. return 0;
  42. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42284
推荐阅读
相关标签
  

闽ICP备14008679号