BoxType.cpp: #ifndef BOXTYPE_CPP #define BOXTYPE_CPP #include \"BoxType.h\" // D
ID: 3723425 • Letter: B
Question
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) { width = hei; }
// TODO: Implement the addition operator overload here. // // //
// TODO: Implement the subtraction operator overload here. // Remember that boxes cannot have negative dimensions. // //
// TODO: Implement the multiplication operator overload here. // // //
// TODO: Implement the division operator overload here. // Remember that division by zero is undefined. // //
// TODO: Implement the greater than operator overload here. // // //
// TODO: Implement the less than operator overload here. // // //
// TODO: Implement the equality operator overload here. // // //
// TODO: Implement the not-equal-to operator overload here. // // //
// TODO: Implement the post-increment operator overload here. // // //
// TODO: Implement the pre-increment operator overload here. // // //
// TODO: Implement the post-decrement operator overload here. // // //
// TODO: Implement the pre-decrement operator overload here. // // //
// TODO: Implement the stream insertion operator overload here. // // // // TODO: Implement the stream extraction operator overload here. // // //
#endif // !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) { width = hei; }
// TODO: Implement the addition operator overload here. // // //
// TODO: Implement the subtraction operator overload here. // Remember that boxes cannot have negative dimensions. // //
// TODO: Implement the multiplication operator overload here. // // //
// TODO: Implement the division operator overload here. // Remember that division by zero is undefined. // //
// TODO: Implement the greater than operator overload here. // // //
// TODO: Implement the less than operator overload here. // // //
// TODO: Implement the equality operator overload here. // // //
// TODO: Implement the not-equal-to operator overload here. // // //
// TODO: Implement the post-increment operator overload here. // // //
// TODO: Implement the pre-increment operator overload here. // // //
// TODO: Implement the post-decrement operator overload here. // // //
// TODO: Implement the pre-decrement operator overload here. // // //
// TODO: Implement the stream insertion operator overload here. // // // // TODO: Implement the stream extraction operator overload here. // // //
#endif // !BOXTYPE_CPP
BoxType.h:
#ifndef BOXTYPE_H #define BOXTYPE_H
#include <iostream>
using namespace std;
class BoxType { // TODO: Provide the prototype to overload the stream insertion operator here. // TODO: Provide the prototype to overload the stream extraction operator here.
public: BoxType(); BoxType(double, double, double); void setLength(double); void setWidth(double); void setHeight(double);
// TODO: Provide the prototype to overload the addition operator here. // TODO: Provide the prototype to overload the subtraction operator here. // TODO: Provide the prototype to overload the multiplication operator here. // TODO: Provide the prototype to overload the division operator here. // TODO: Provide the prototype to overload the greater than operator here. // TODO: Provide the prototype to overload the less than operator here. // TODO: Provide the prototype to overload the equality operator here. // 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. // TODO: Provide the prototype to overload the pre-decrement operator here. // TODO: Provide the prototype to overload the post-decrement operator here.
private: double length; double width; double height; };
#endif // BOXTYPE_H #ifndef BOXTYPE_H #define BOXTYPE_H
#include <iostream>
using namespace std;
class BoxType { // TODO: Provide the prototype to overload the stream insertion operator here. // TODO: Provide the prototype to overload the stream extraction operator here.
public: BoxType(); BoxType(double, double, double); void setLength(double); void setWidth(double); void setHeight(double);
// TODO: Provide the prototype to overload the addition operator here. // TODO: Provide the prototype to overload the subtraction operator here. // TODO: Provide the prototype to overload the multiplication operator here. // TODO: Provide the prototype to overload the division operator here. // TODO: Provide the prototype to overload the greater than operator here. // TODO: Provide the prototype to overload the less than operator here. // TODO: Provide the prototype to overload the equality operator here. // 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. // TODO: Provide the prototype to overload the pre-decrement operator here. // TODO: Provide the prototype to overload the post-decrement operator here.
private: double length; double width; double height; };
#endif // BOXTYPE_H
Main.cpp:
#include "BoxType.h"
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; "; cout << box1 + box2; cout << "cout << box1 - box2; "; cout << box1 - box2; cout << "cout << box1 * box2; "; cout << box1 * box2; cout << "cout << box1 / box2; "; cout << box1 / box2;
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; "; cout << ++box1; cout << "cout << --box2; "; cout << --box2;
BoxType box3 = box2++; cout << "BoxType box3 = box2++;" << endl; cout << "cout << box3; "; cout << box3; cout << "cout << box2; "; cout << box2;
return 0; } #include "BoxType.h"
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; "; cout << box1 + box2; cout << "cout << box1 - box2; "; cout << box1 - box2; cout << "cout << box1 * box2; "; cout << box1 * box2; cout << "cout << box1 / box2; "; cout << box1 / box2;
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; "; cout << ++box1; cout << "cout << --box2; "; cout << --box2;
BoxType box3 = box2++; cout << "BoxType box3 = box2++;" << endl; cout << "cout << box3; "; cout << box3; cout << "cout << box2; "; cout << box2;
return 0; } 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 Box Type class by performing arithmetic operations on their respective dimensions. Consider that boxI's length 4, width 5, and height 9 Consider that box2's length 3, width 2, and height 3 BoxType box3 box-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 bol's length 4, width-5, and height-9. box1++; boxl now has a length of S and a width of 6 and a height of 10. Write the BoxType class and overload the following operators (using either member or non member 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.
Explanation / Answer
Header file:
#ifndef BOXTYPE_H
#define BOXTYPE_H
#include <iostream>
using namespace std;
class BoxType
{
// TODO: Provide the prototype to overload the stream insertion operator here.
// TODO: Provide the prototype to overload the stream extraction operator here.
public:
BoxType();
BoxType(double, double, double);
void setLength(double);
void setWidth(double);
void setHeight(double);
friend ostream & operator <<(ostream &out, const BoxType &temp);
friend istream & operator >>(istream &in, BoxType &temp);
// overloaded the arithmetic operator.
BoxType operator+(const BoxType &b1);
BoxType operator-(const BoxType &b1);
BoxType operator*(const BoxType &b1);
BoxType operator/(const BoxType &b1);
//the prototype to overload the greater than,less than, equality and not-equal-to operator.
int operator>(const BoxType &b1);
int operator<(const BoxType &b1);
int operator==(const BoxType &b1);
int operator!=(const BoxType &b1);
// the prototype to overload the pre-increment, post-increment, pre-decrement, post-decrement operator.
BoxType operator++();//prefix increment
BoxType operator--();//prefix decrement
BoxType operator++(int);//postfix increment
BoxType operator--(int);//postfix decrement
private:
double length;
double width;
double height;
};
#endif /* BOXTYPE_H */
BoxType.cpp
#ifndef BOXTYPE_CPP
#define BOXTYPE_CPP
#include <valarray>
#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;
}
//insertion
std::ostream& operator << (std::ostream& out, const BoxType& temp){
out<<"Length :"<<temp.length;
out<<" height :"<<temp.height;
out<<" width :"<<temp.width;
return out;
}
//extraction
std::istream& operator >> (std::istream &in, BoxType &temp){
in>>temp.length;
in>>temp.height;
in>>temp.width;
return in;
}
// addition operator overload.
BoxType BoxType::operator+(const BoxType &b1){
BoxType b3;
b3.length = length + b1.length;
b3.width = width + b1.width;
b3.height = height + b1.width;
return b3;
}
//subtraction operator overload .
BoxType BoxType::operator-(const BoxType &b1){
BoxType b3;
b3.length = length - b1.length;
b3.width = width - b1.width;
b3.height = height - b1.height;
return b3;
}
// multiplication operator overload
BoxType BoxType::operator*(const BoxType &b1){
BoxType b3;
b3.length = length * b1.length;
b3.width = width * b1.width;
b3.height = height * b1.height;
return b3;
}
//division operator overload
BoxType BoxType::operator/(const BoxType &b1){
BoxType b3;
b3.length = length / b1.length;
b3.width = width / b1.width;
b3.height = height / b1.height;
return b3;
}
// TODO: Implement the greater than operator overload here.
int BoxType::operator>(const BoxType &b1){
if((length>b1.length)&&(width>b1.width)&&(height>b1.height)){
return(1);
}
return (0);
}
// less than operator overload .
int BoxType::operator<(const BoxType &b1){
if((length<b1.length)&&(width<b1.width)&&(height<b1.height)){
return(1);
}
return (0);
}
//equality operator overload.
int BoxType::operator==(const BoxType &b1){
if((length==b1.length)&&(width==b1.width)&&(height==b1.height)){
return(1);
}
return (0);
}
// not-equal-to operator overload.
int BoxType::operator!=(const BoxType &b1){
if((length!=b1.length)&&(width!=b1.width)&&(height!=b1.height)){
return(1);
}
return (0);
}
// post-increment operator
BoxType BoxType::operator++(int){
BoxType temp( length, width, height);
height++;
length++;
width++;
return temp;
}
// pre-increment operator overload
BoxType BoxType::operator++(){
height++;
length++;
width++;
return(*this);
}
// post-decrement operator overload
BoxType BoxType::operator--(int){
BoxType temp( length, width, height);
height--;
length--;
width--;
return temp;
}
// pre-decrement operator overload
BoxType BoxType:: operator--(){
height--;
length--;
width--;
return(*this);
}
#endif // !BOXTYPE_CPP
Driver .cpp:
#include "BoxType.h"
int main()
{
BoxType box1;
BoxType box2;
cout << " Space separated, enter the length, width, and height of box1: ";
cin >> box1;
cout << " Space separated, enter the length, width, and height of box2: ";
cin >> box2;
cout << " box1 :";
cout << box1;
cout << " box2 :";
cout << box2;
cout << " box1 + box2 :: ";
cout << box1 + box2;
cout << " box1 - box2 ::";
cout << box1 - box2;
cout << " box1 * box2 ::";
cout << box1 * box2;
cout << " box1 / box2 :";
cout << box1 / box2;
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 << " ++box1: ";
cout << ++box1;
cout << " --box2 :" ;
cout << --box2;
BoxType box3 = box2++;
cout << " BoxType box3 = box2++;" << endl;
cout << " box3 :";
cout << box3;
cout << " box2 :";
cout << box2;
return 0;
}
Sample Output:
Space separated, enter the length, width, and height of box1: 2 4 9
Space separated, enter the length, width, and height of box2: 1 2 3
box1 :Length :2 height :4 width :9
box2 :Length :1 height :2 width :3
box1 + box2 :: Length :3 height :7 width :12
box1 - box2 ::Length :1 height :2 width :6
box1 * box2 ::Length :2 height :8 width :27
box1 / box2 :Length :2 height :2 width :3
box1 > box2
box1 != box2
++box1: Length :3 height :5 width :10
--box2 :Length :0 height :1 width :2
BoxType box3 = box2++;
box3 :Length :0 height :1 width :2
box2 :Length :1 height :2 width :3
RUN SUCCESSFUL (total time: 8s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.