Advertisement
gromoff97

Practice. Task 18

Jul 1st, 2020
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. // box.hpp
  2. #ifndef _BOX_H_
  3. #define _BOX_H_
  4.  
  5. class box
  6. {
  7. private:
  8.     unsigned int width;
  9.     unsigned int length;
  10.     unsigned int height;
  11.  
  12. public:
  13.     box(int width, int length, int height);
  14.     box(const box &b);
  15.     static box init(int width, int length, int height);
  16.     unsigned int get_width();
  17.     unsigned int get_length();
  18.     unsigned int get_height();
  19. };
  20.  
  21. #endif
  22.  
  23. //box.cpp
  24. #include "box.hpp"
  25. #include <stdexcept>
  26.  
  27. box::box(const box &b)
  28. {
  29.     this->width = b.width;
  30.     this->length = b.length;
  31.     this->height = b.height;
  32. }
  33.  
  34. box::box(int width, int length, int height)
  35. {
  36.     if (width <= 0)  throw std::invalid_argument("Высота коробки должна быть положительной");  
  37.     if (length <= 0) throw std::invalid_argument("Длина коробки должна быть положительной");
  38.     if (height <= 0) throw std::invalid_argument("Высота коробки должна быть положительной");
  39.  
  40.     this->width = width;
  41.     this->length = length;
  42.     this->height = height;
  43. }
  44.  
  45. box box::init(int width, int length, int height)
  46. {
  47.     return box(width, length, height);
  48. }
  49.  
  50. unsigned int box::get_width()
  51. {
  52.     return this->width;
  53. }
  54.  
  55. unsigned int box::get_length()
  56. {
  57.     return this->length;
  58. }
  59.  
  60. unsigned int box::get_height()
  61. {
  62.     return this->height;
  63. }
  64.  
  65. // main.cpp
  66. #include "box.hpp"
  67. #include <iostream>
  68.  
  69. void print_volume(box* b)
  70. {
  71.     std::cout << b->get_length() * b->get_width() * b->get_height() << std::endl;
  72. }
  73.  
  74. int main(int argc, char const *argv[])
  75. {
  76.     box boxes[4] = {
  77.         box::init(1, 1, 2),
  78.         box::init(2, 3, 4),
  79.         box::init(4, 5, 6),
  80.         box::init(7, 8, 9)
  81.     };
  82.  
  83.     for (int i = 3; i >= 0; i--) print_volume(&boxes[i]);
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement