Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // box.hpp
- #ifndef _BOX_H_
- #define _BOX_H_
- class box
- {
- private:
- unsigned int width;
- unsigned int length;
- unsigned int height;
- public:
- box(int width, int length, int height);
- box(const box &b);
- static box init(int width, int length, int height);
- unsigned int get_width();
- unsigned int get_length();
- unsigned int get_height();
- };
- #endif
- //box.cpp
- #include "box.hpp"
- #include <stdexcept>
- box::box(const box &b)
- {
- this->width = b.width;
- this->length = b.length;
- this->height = b.height;
- }
- box::box(int width, int length, int height)
- {
- if (width <= 0) throw std::invalid_argument("Высота коробки должна быть положительной");
- if (length <= 0) throw std::invalid_argument("Длина коробки должна быть положительной");
- if (height <= 0) throw std::invalid_argument("Высота коробки должна быть положительной");
- this->width = width;
- this->length = length;
- this->height = height;
- }
- box box::init(int width, int length, int height)
- {
- return box(width, length, height);
- }
- unsigned int box::get_width()
- {
- return this->width;
- }
- unsigned int box::get_length()
- {
- return this->length;
- }
- unsigned int box::get_height()
- {
- return this->height;
- }
- // main.cpp
- #include "box.hpp"
- #include <iostream>
- void print_volume(box* b)
- {
- std::cout << b->get_length() * b->get_width() * b->get_height() << std::endl;
- }
- int main(int argc, char const *argv[])
- {
- box boxes[4] = {
- box::init(1, 1, 2),
- box::init(2, 3, 4),
- box::init(4, 5, 6),
- box::init(7, 8, 9)
- };
- for (int i = 3; i >= 0; i--) print_volume(&boxes[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement