Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #define MSG_POSITIVE "Dimensions have to be positive"
- #define MSG_UNEXPECTED "Unexpected error occurred"
- using namespace std;
- class Box {
- private:
- int width;
- int height;
- int depth;
- public:
- // constructors
- Box(int x, int y, int z){
- setWidth(x);
- setHeight(y);
- setDepth(z);
- }
- // setters
- public:
- void setWidth(int width){
- if(width > 0)
- this->width = width;
- else
- throw invalid_argument(MSG_POSITIVE);
- }
- void setHeight(int height){
- if(height > 0)
- this->height = height;
- else
- throw invalid_argument(MSG_POSITIVE);
- }
- void setDepth(int depth){
- if(depth > 0)
- this->depth = depth;
- else
- throw invalid_argument(MSG_POSITIVE);
- }
- public:
- // getters
- int getWidth(){
- return this->width;
- }
- int getHeight(){
- return this->height;
- }
- int getDepth(){
- return this->depth;
- }
- string toString(){
- string temp = "(";
- temp += to_string(getWidth());
- temp += ", " + to_string(getHeight());
- temp += ", " + to_string(getDepth());
- temp += ")";
- return temp;
- }
- };
- int main() {
- try {
- Box box(1, 2, 3);
- cout << box.toString() << endl;
- box.setWidth(10);
- cout << box.toString() << endl;
- box.setWidth(-10);
- cout << box.toString() << endl;
- } catch(exception &e){
- cout << e.what() << endl;
- } catch(...){
- cout << MSG_UNEXPECTED << endl;
- }
- try {
- Box box(10, -20, 30);
- cout << box.toString() << endl;
- box.setWidth(10);
- cout << box.toString() << endl;
- box.setWidth(-10);
- cout << box.toString() << endl;
- } catch(exception &e){
- cout << e.what() << endl;
- } catch(...){
- cout << MSG_UNEXPECTED << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement