C++ HW10-1 for WallPower

若有長方體物件分別為,一號長方體c1(2,3,3),二號長方體c2(5,5,5),求兩者體積。
參考輸出如下。




  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Couboid
  5. {
  6. public:
  7. Couboid(int x, int y, int z);
  8. ~Couboid();
  9. int volume() { return (*x)*(*y)*(*z); }
  10. private:
  11. int *x, *y, *z;
  12. };
  13.  
  14. Couboid::Couboid(int a, int b, int c)
  15. {
  16. x = new int;
  17. y = new int;
  18. z = new int;
  19. *x = a;
  20. *y = b;
  21. *z = c;
  22. }
  23.  
  24. Couboid::~Couboid()
  25. {
  26. delete x;
  27. delete y;
  28. delete z;
  29. }
  30.  
  31. int main()
  32. {
  33. Couboid c1(2, 3, 3), c2(5, 5, 5);
  34. cout << "一號長方體體積:" << c1.volume() << endl;
  35. cout << "二號長方體體積:" << c2.volume() << endl;
  36. system("pause");
  37. return 0;
  38. }

留言

這個網誌中的熱門文章

C# 井字遊戲