Question 1: • The BoxType class holds the length, width, and height of a box. •
ID: 3723754 • Letter: Q
Question
Question 1: • The BoxType class holds the length, width, and height of a box. • Instances of the BoxType class are considered equal if their respective volumes are equal. • Arithmetic can be performed on instances of the BoxType class by performing arithmetic operations on their respective dimensions. Consider that box1’s length = 4, width = 5, and height = 9. Consider that box2’s length = 3, width = 2, and height = 3. BoxType box3 = box1 – box2; box3 will have a length of 1 and a width of 3 and a height of 6. • BoxType objects can be incremented/decremented by increasing/decreasing their respective dimensions by 1. Consider that box1’s length = 4, width = 5, and height = 9. box1++; box1 now has a length of 5 and a width of 6 and a height of 10. Write the BoxType class and overload the following operators (using either member or nonmember functions): Addition, Subtraction, Multiplication, Division, Greater Than, Less Than, Equal To, Not Equal To, Pre-Increment, Pre-Decrement, Post-Increment, and Post-Decrement. Keep in mind that a box cannot have negative dimensions. If any calculation results in a negative dimension, set all dimensions to the default 0. • Next, overload the stream insertion and stream extraction operators, so the following main test function produces the shown output. Your BoxType class and operator overloads should work with the included main function. Please don’t alter main.
BoxType.h--------
--------BoxType.cpp-----
-----main.cpp----
Sample Output: If you use the highlighted input, your output should match the image provided here. Select CAUsers jlatelSourcelReposlLab141DebugiLab14.exe Space separated, enter the length, width, and height of box1:10 15 20 coutExplanation / Answer
//BoxType.h
#ifndef BOXTYPE_H
#define BOXTYPE_H
// #include
using namespace std;
class BoxType
{
// TODO: Provide the prototype to overload the stream insertion operator here.
friend istream & operator >> (istream &in, BoxType &c);
// TODO: Provide the prototype to overload the stream extraction operator here.
friend ostream & operator << (ostream &out, BoxType &box);
public:
BoxType();
BoxType(double, double, double);
double getLength();
double getHeight();
double getWidth();
void setLength(double);
void setWidth(double);
void setHeight(double);
// TODO: Provide the prototype to overload the addition operator here.
friend BoxType operator + (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the subtraction operator here.
friend BoxType operator - (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the multiplication operator here.
friend BoxType operator * (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the division operator here.
friend BoxType operator / (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the greater than operator here.
friend bool operator > (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the less than operator here.
friend bool operator < (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the equality operator here.
friend bool operator == (BoxType &b, BoxType &b1);
// TODO: Provide the prototype to overload the not-equal-to operator here.
// TODO: Provide the prototype to overload the pre-increment operator here.
// TODO: Provide the prototype to overload the post-increment operator here.
friend BoxType operator ++ (BoxType &b);
// TODO: Provide the prototype to overload the pre-decrement operator here.
friend BoxType operator -- (BoxType &b);
// TODO: Provide the prototype to overload the post-decrement operator here.
private:
double length;
double width;
double height;
};
#endif // BOXTYPE_H
//BoxType.cpp
#ifndef BOXTYPE_CPP
#define BOXTYPE_CPP
#include "BoxType.h"
// Default Constructor
BoxType::BoxType()
{
length = 0;
width = 0;
height = 0;
}
// Parameterized Constructor
BoxType::BoxType(double len, double wid, double hei)
{
setLength(len);
setWidth(wid);
setHeight(hei);
}
void BoxType::setLength(double len)
{
length = len;
}
void BoxType::setWidth(double wid)
{
width = wid;
}
void BoxType::setHeight(double hei)
{
height = hei;
}
double BoxType::getLength()
{
return length;
}
double BoxType::getHeight()
{
return height;
}
double BoxType::getWidth()
{
return width;
}
// TODO: Implement the addition operator overload here.
BoxType operator+(BoxType &box1, BoxType &box2)
{
return BoxType(box1.getLength() + box2.getLength(), box1.getWidth() + box2.getWidth(), box1.getHeight() + box2.getHeight());
}
// TODO: Implement the subtraction operator overload here.
// Remember that boxes cannot have negative dimensions.
BoxType operator-(BoxType &box1, BoxType &box2)
{
return BoxType(box1.getLength() - box2.getLength(), box1.getWidth() - box2.getWidth(), box1.getHeight() - box2.getHeight());
}
// TODO: Implement the multiplication operator overload here.
BoxType operator*(BoxType &box1, BoxType &box2)
{
return BoxType(box1.getLength() * box2.getLength(), box1.getWidth() * box2.getWidth(), box1.getHeight() * box2.getHeight());
}
// TODO: Implement the division operator overload here.
// Remember that division by zero is undefined.
BoxType operator/(BoxType &box1, BoxType &box2)
{
return BoxType(box1.getLength() / box2.getLength(), box1.getWidth() / box2.getWidth(), box1.getHeight() / box2.getHeight());
}
// TODO: Implement the greater than operator overload here.
bool operator>(BoxType &box1, BoxType &box2)
{
double a1 = box1.getHeight() * box1.getLength() + box1.getWidth();
double a2 = box2.getHeight() * box2.getLength() + box2.getWidth();
return a1 > a2;
}
// TODO: Implement the less than operator overload here.
bool operator<(BoxType &box1, BoxType &box2)
{
double a1 = box1.getHeight() * box1.getLength() + box1.getWidth();
double a2 = box2.getHeight() * box2.getLength() + box2.getWidth();
return a1 < a2;
}
// TODO: Implement the equality operator overload here.
bool operator == (BoxType &box1, BoxType &box2)
{
double a1 = box1.getHeight() * box1.getLength() + box1.getWidth();
double a2 = box2.getHeight() * box2.getLength() + box2.getWidth();
return a1 == a2;
}
// TODO: Implement the not-equal-to operator overload here.
bool operator != (BoxType &box1, BoxType &box2){
double a1 = box1.getHeight()*box1.getLength()+box1.getWidth();
double a2 = box2.getHeight()*box2.getLength()+box2.getWidth();
return a1!=a2;
}
// TODO: Implement the post-increment operator overload here.
BoxType operator ++ (BoxType &box){
box.setWidth(box.getWidth()+1);
box.setHeight(box.getHeight()+1);
box.setLength(box.getLength()+1);
return box;
}
// TODO: Implement the post-decrement operator overload here.
BoxType operator -- (BoxType &box){
box.setWidth(box.getWidth()-1);
box.setHeight(box.getHeight()-1);
box.setLength(box.getLength()-1);
return box;
}
// TODO: Implement the pre-decrement operator overload here.
//
//
//
// TODO: Implement the stream insertion operator overload here.
istream &operator>>(istream &in, BoxType &box)
{
// double length, width, height;
in >> box.length >> box.width >> box.height;
return in;
}
// TODO: Implement the stream extraction operator overload here.
ostream &operator<<(ostream &out, BoxType &box)
{
out << "Length: " << box.length << ", Width: " << box.width << ", Height: " << box.height << endl;
return out;
}
#endif // !BOXTYPE_CPP
//main.cpp
#include<iostream>
#include "BoxType.cpp"
using namespace std;
int main()
{
BoxType box1;
BoxType box2;
cout << "Space separated, enter the length, width, and height of box1: ";
cin >> box1;
cout << "cout << box1; ";
cout << box1;
cout << "Space separated, enter the length, width, and height of box2: ";
cin >> box2;
cout << "cout << box2; ";
cout << box2;
cout << "box1 + box2; ";
BoxType box3 = box1+box2;
cout << box3;
cout << "cout << box1 - box2; ";
box3 = box1-box2;
cout << box3;
cout << "cout << box1 * box2; ";
box3 = box1 * box2;
cout << box3;
cout << "cout << box1 / box2; ";
box3 = box1 / box2;
cout << box3;
if (box1 > box2)
cout << "box1 > box2" << endl;
if (box1 < box2)
cout << "box1 < box2" << endl;
if (box1 == box2)
cout << "box1 == box2" << endl;
if (box1 != box2)
cout << "box1 != box2" << endl;
cout << "cout << ++box1; ";
box3 = ++box1;
cout << box3;
cout << "cout << --box2; ";
box3 = --box2;
cout << box3;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.