Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is in C++. It is a program demonstrating how a class can be used to modify a box in different ways/throw invalid arguments. I

This is in C++. It is a program demonstrating how a class can be used to modify a box in different ways/throw invalid arguments. I am having trouble understanding the code as the teacher did not include comments, can someone please add some detailed ones to help?

#include #include using namespace std;

class Box {

double height, width, depth;

public: Box() { height = width = depth = 1.0; } Box(double h, double w, double d) { height = h; width = w; depth = d; }

void resize(double h, double w, double d) { height = h; width = w; depth = d; }

void set_height(double h) { height = h; if(h < 0.01) { throw invalid_argument("Height is less than 0.01."); } } void set_width(double w) { width = w; if(w < 0.01) { throw invalid_argument("Width is less than 0.01."); } } void set_depth(double d) { depth = d; if(d < 0.01) { throw invalid_argument("Depth is less than 0.01."); } }

double get_height() { return height; } double get_width() { return width; } double get_depth() { return depth; }

double volume() { return height * width * depth; }

void to_string() { cout << "Height:\t" << height << endl; cout << "Width :\t" << width << endl; cout << "Depth :\t" << depth << endl; cout << endl; } };

int main() { Box b1, b2(9, 8, 3); cout << "Box b1:" << endl; b1.to_string(); cout << "Box b2:" << endl; b2.to_string();

cout << "Volume of b1: " << b1.volume() << endl; cout << "Volume of b2: " << b2.volume() << endl; cout << endl;

cout << "Changing width of b2 to 0.3" << endl; b2.set_width(0.90); cout << "Changing width of b2 to 0.90, width: " << b2.get_width() << endl; cout << "Volume: " << b2.volume() << endl; cout << endl;

b1.resize(2.3, 1.5, 5.2); cout << "After resizing b1:" << endl; b1.to_string();

cout << "New volume:" << endl; cout << "Volume: " << b1.volume() << endl; try { cout << "If width is set under 0.01:" << endl; b1.set_width(0.001); } catch(invalid_argument e) { cout << "0.01 is the minimum width for the box!" << endl; } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

How does interconnectivity change how we live and work?

Answered: 1 week ago