Question

This is in C++. It is a program demonstrating how a class can be used to...

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<iostream>
#include<stdexcept>
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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<stdexcept>
using namespace std;
//a class Box consisting of data members height,width and depth declared as private which can't be accesible outside class
class Box
{
double height, width, depth;
public:

//default constructor which initializes width,height & depth to 1.0
Box()
{
height = width = depth = 1.0;
}

//parameterized constructor used for initializing data members of class
Box(double h, double w, double d)
{
height = h;
width = w;
depth = d;
}
//increasing dimensions for the box by using resize function and storing it in height,width & depth
void resize(double h, double w, double d)
{
height = h;
width = w;
depth = d;
}
//used for initializing height value
void set_height(double h)
{
height = h;
//checks height value and if it is less than 0.01 then an invalid_argument exception will be thrown with a message
// where invalid_argument is class which defines different types of objects thrown as exception by reporting an invalid argument
if(h < 0.01)
{

throw invalid_argument("Height is less than 0.01.");
}
}
//used for initializing width value
void set_width(double w)
{
width = w;
//checks width value and if it is less than 0.01 then an invalid_argument exception will be thrown with a message
// where invalid_argument is class which defines different types of objects thrown as exception by reporting an invalid argument
if(w < 0.01)
{

throw invalid_argument("Width is less than 0.01.");
}
}
//used for initializing depth value

void set_depth(double d)
{
depth = d;
//checks width value and if it is less than 0.01 then an invalid_argument exception will be thrown with a message
// where invalid_argument is class which defines different types of objects thrown as exception by reporting an invalid argument
if(d < 0.01)
{

throw invalid_argument("Depth is less than 0.01.");
}
}
//returns height
double get_height()
{
return height;
}

double get_width()
{
return width;
}

double get_depth()
{
return depth;
}
//returns volume of box
double volume()
{

return height * width * depth;
}
//print the contents of object
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);//Box b1 calling default constructor and Box b2 calling parameterized constructor

cout << "Box b1:" << endl;
b1.to_string(); //print b1 contents

cout << "Box b2:" << endl;
b2.to_string();//print b2 contents

cout << "Volume of b1: " << b1.volume() << endl; //calculate volume of boxes b1 & b2
cout << "Volume of b2: " << b2.volume() << endl;
cout << endl;

cout << "Changing width of b2 to 0.3" << endl;
b2.set_width(0.90);//changing width of second box

cout << "Changing width of b2 to 0.90, width: " << b2.get_width() << endl;////changing width of second box again
cout << "Volume: " << b2.volume() << endl; //calling second box volume
cout << endl;
  
b1.resize(2.3, 1.5, 5.2); //resizing first box

cout << "After resizing b1:" << endl;
b1.to_string(); //printing box b1 contents after resizing its dimensions
cout << "New volume:" << endl;
cout << "Volume: " << b1.volume() << endl; //calculating volume of new box


try
{
cout << "If width is set under 0.01:" << endl;
b1.set_width(0.001);
}
//exception handler catches the exception thrown by set_width() function and handles it
catch(invalid_argument e)
{
cout << "0.01 is the minimum width for the box!" << endl;
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
This is in C++. It is a program demonstrating how a class can be used to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • I need help solving this basic C++ question. I need help fixing my code.   Question: Design...

    I need help solving this basic C++ question. I need help fixing my code.   Question: Design a class called a box that represents a box. Box classes have variables such as the length of the box, width, and height. 1. Member variables shall be dedicated members. 2. Define the creator of the Box Class. The creator may receive all of the data and may not receive any. 3. Add accessor and creator 4. Add an empty function, which indicates whether...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • Consider the following declaration for a class that will be used to represent rectangles. public class...

    Consider the following declaration for a class that will be used to represent rectangles. public class Rectangle { private double height; private double width; public Rectangle() { height = 2.0; width = 1.0; } public Rectangle(double w, double h) { height = h; width = w; } public double getHeight() { return height; } public double getWidth() { return width; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } //Other...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • Use only C++ for all function Introduce a call-by-value function that computes the volume of a...

    Use only C++ for all function Introduce a call-by-value function that computes the volume of a box. Hint: Length, width, and height of a box is needed. Introduce a call-by-reference function with void output. that computes the square of an integer. Please double check the value of the function input before and after function call. Introduce a call-by-pointer function with void output. that computes the square of an integer. Please double check the value of the function input before and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT