Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Help. Please add comments to the following program. #ifndef BOX_H #define BO

ID: 3755479 • Letter: C

Question

C++ Help. Please add comments to the following program.

#ifndef BOX_H
#define BOX_H

class Box
{
public:
   Box(int length, int width, int height);
   Box();
   Box operator+(const Box& b1) const;
   Box operator-(const Box& b1) const;
   Box operator*(const Box& b1) const;
   bool operator>(const Box& b1) const;
   Box operator++();
   Box operator++(int);
   bool operator<(const Box& b1) const;
   bool operator<=(const Box& b1) const;
   bool operator>=(const Box& b1) const;
   bool operator==(const Box& b1) const;
   bool operator!=(const Box& b1) const;

   void setLength(double);
   void setWidth(double);
   void setHeight(double);

   friend std::ostream& operator<<(std::ostream& dout, const Box& b);
private:
   // Declaring variables
   int length;
   int width;
   int height;

};
#endif


................................................................................................................

#include <iostream>
#include <sstream>
using namespace std;
#include "box.h"

Box::Box(int length, int width, int height)
{

   setLength(length);
   setWidth(width);
   setHeight(height);

}
void Box::setLength(double length)
{
   if (length < 0)
       this->length = 0;
   else
       this->length = length;
}
void Box::setWidth(double width)
{
   if (width < 0)
       this->width = 0;
   else
       this->width = width;
}
void Box::setHeight(double height)
{
   if (height < 0)
       this->height = 0;
   else
       this->height = height;
}

Box::Box()
{

}
Box Box::operator+(const Box& b1) const
{
   double l = b1.length + length;
   double w = b1.width + width;
   double h = b1.height + height;
   Box b(l, w, h);
   return b;
}
Box Box::operator-(const Box& b1) const
{
   Box b;
   b.setLength(length - b1.length);
   b.setWidth(width - b1.width);
   b.setHeight(height - b1.height);

   return b;
}
Box Box::operator*(const Box& b1) const
{
   double l = length * b1.length;
   double w = width * b1.width;
   double h = height * b1.height;
   Box b(l, w, h);
   return b;
}
bool Box::operator>(const Box& b1) const
{
   if (length*width*height > b1.length*b1.width*b1.height)
       return true;
   else
       return false;
}
Box Box::operator++()
{
   double l = ++length;
   double w = ++width;
   double h = ++height;
   Box b(l, w, h);
   return b;
}

bool Box::operator<(const Box& b1) const
{
   if (length*width*height < b1.length*b1.width*b1.height)
       return true;
   else
       return false;
}
bool Box::operator<=(const Box& b1) const
{
   if (length*width*height <= b1.length*b1.width*b1.height)
       return true;
   else
       return false;
}
bool Box::operator>=(const Box& b1) const
{
   if (length*width*height >= b1.length*b1.width*b1.height)
       return true;
   else
       return false;
}
bool Box::operator==(const Box& b1) const
{
   if (length == b1.length && width == b1.width && height == b1.height)
       return true;
   else
       return false;
}
bool Box::operator!=(const Box& b1) const
{
   if (length == b1.length && width == b1.width && height == b1.height)
       return false;
   else
       return true;
}

ostream& operator<<(ostream& dout, const Box& b)
{
   dout << "Length is " << b.length << ". Width is " << b.width << ". Height is " << b.height << "." << endl;
   return dout;
}

Box Box::operator++(int)
{
   const Box old(*this);
   ++(*this);
   return old;

   // b.length++;
   // b.width++;
   // b.height++;
   //return b;
}

Explanation / Answer

//creating the header file

#ifndef BOX_H

#define BOX_H

//creating the class Box in header

class Box

{

                //public methods in Box class

                public:

                                //constructors

                                //para metrized constructor declaration

                                Box(int length, int width, int height);

                                //default constructor declaration

                                Box();

                                //operator overloading

                                //addition operator overloading function declaration

                                Box operator+(const Box& b1) const;

                                //substraction operator overloading function declaration

                                Box operator-(const Box& b1) const;

                                //multiplication operator overloading function declaration

                                Box operator*(const Box& b1) const;

                                //division operator overloading function declaration

                                bool operator>(const Box& b1) const;

                                //increment operator overloading function declaration

                                Box operator++();

                                //parametrized increment operator overloading function declaration

                                Box operator++(int);

                                //relation operators

                                //lessthan operator overloading function declaration

                                bool operator<(const Box& b1) const;

                                //lessthan or equal operator overloading function declaration

                                bool operator<=(const Box& b1) const;

                                //greaterthan or equal operator overloading function declaration

                                bool operator>=(const Box& b1) const;

                                //equality operator overloading function declaration

                                bool operator==(const Box& b1) const;

                                //inequality operator overloading function declaration

                                bool operator!=(const Box& b1) const;

                                //setter methods

                                void setLength(double);

                                void setWidth(double);

                                void setHeight(double);

                                //stdout operation declaration

                                friend std::ostream& operator<<(std::ostream& dout, const Box& b);

                //private variables declaration

                private:

                                // Declaring variables

                                int length;

                                int width;

                                int height;

};

#endif

................................................................................................................

//main program file

//including the libraries

#include <iostream>

#include <sstream>

//declaring the namespace

using namespace std;

//including the header file

#include "box.h"

//definition of the methods declared in header

//constructors

//para metrized constructor declaration

Box::Box(int length, int width, int height)

{

                                //setting the variable values

                                setLength(length);

                                setWidth(width);

                                setHeight(height);

}

//setter methods

//setter function for length

void Box::setLength(double length)

{

                                //assigning 0 if the input is negative

                                if (length < 0)

                                                                this->length = 0;

                                //assigning the given length if positive

                                else

                                                                this->length = length;

}

//setter function for width

void Box::setWidth(double width)

{

                                //assigning 0 if the input is negative

                                if (width < 0)

                                                                this->width = 0;

                                //assigning the given width if positive

                                else

                                                                this->width = width;

}

//setter function for height

void Box::setHeight(double height)

{

                                //assigning 0 if the input is negative

                                if (height < 0)

                                                                this->height = 0;

                                //assigning the given height if positive

                                else

                                                                this->height = height;

}

//default constructor definition

Box::Box()

{

                //blank definition

}

//operators overloading functions definitions

//addition operator overloading function definition

Box Box::operator+(const Box& b1) const

{

                                //updating all values

                                double l = b1.length + length;

                                double w = b1.width + width;

                                double h = b1.height + height;

                                //creating the new Box object with new values

                                Box b(l, w, h);

                                //returning the new Box object

                                return b;

}

//substraction operator overloading function definition

Box Box::operator-(const Box& b1) const

{

                                //creating the new Box object

                                Box b;

                                //assigning the calculated values to the new box object

                                b.setLength(length - b1.length);

                                b.setWidth(width - b1.width);

                                b.setHeight(height - b1.height);

                                //returning the new Box object

                                return b;

}

//multiplication operator overloading function definition

Box Box::operator*(const Box& b1) const

{

                                //updating all values

                                double l = length * b1.length;

                                double w = width * b1.width;

                                double h = height * b1.height;

                                //creating the new Box object with new values

                                Box b(l, w, h);

                                //returning the new Box object

                                return b;

}

//greater than   operator overloading function definition

bool Box::operator>(const Box& b1) const

{

                                //checking the volumes of each box

                                //if the current box is greater than the input box

                                if (length*width*height > b1.length*b1.width*b1.height)

                                                                //returning true

                                                                return true;

                                //if the current box is not greater than the input box

                                else

                                                                //returning false

                                                                return false;

}

//increment operator overload

Box Box::operator++()

{

                                //incrementing each value of Box

                                double l = ++length;

                                double w = ++width;

                                double h = ++height;

                                //creating the new Box object with new values

                                Box b(l, w, h);

                                //returning the new Box object

                                return b;

}

//lessthan operator overloading function definition

bool Box::operator<(const Box& b1) const

{

                                //checking the volumes of each box

                                //if the current box is less than the input box

                                if (length*width*height < b1.length*b1.width*b1.height)

                                                                return true;

                                //if the current box is not less than input box

                                else

                                                                return false;

}

//lessthan or equal operator overloading function

bool Box::operator<=(const Box& b1) const

{

                                //checking the volumes of each box

                                //if the current box is less than or euqal the input box

                                if (length*width*height <= b1.length*b1.width*b1.height)

                                                                return true;

                                //if the current box is greater than the input box

                                else

                                                                return false;

}

//greaterthan or equal operator overloading function definiton

bool Box::operator>=(const Box& b1) const

{

                                //checking the volumes of each box

                                //if the current box is greater than or equal the input box

                                if (length*width*height >= b1.length*b1.width*b1.height)

                                                                return true;

                                //if the current box is less than the input box

                                else

                                                                return false;

}

//equality operator overloading function definition

bool Box::operator==(const Box& b1) const

{

                                //checking the values of each box

                                //if the current box values are same as the input box values

                                if (length == b1.length && width == b1.width && height == b1.height)

                                                                return true;

                                //if the current box values are not same as the input box values

                                else

                                                                return false;

}

//inequality operator overloading function definition

bool Box::operator!=(const Box& b1) const

{

                                //checking the values of each box

                                //if the current box values are not same as the input box values

                                if (length == b1.length && width == b1.width && height == b1.height)

                                                                return false;

                                //if the current box values are same as the input box values

                                else

                                                                return true;

}

//overloading function of stdout

ostream& operator<<(ostream& dout, const Box& b)

{

                                //printing the values of the box

                                dout << "Length is " << b.length << ". Width is " << b.width << ". Height is " << b.height << "." << endl;

                                return dout;

}

//parametrized increment operator overloading function definition

Box Box::operator++(int)

{

                                const Box old(*this);

                                ++(*this);

                                return old;

                                // b.length++;

                                // b.width++;

                                // b.height++;

                                //return b;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote