Objectives: 1. Understanding Operator Overloading in C++. 2. Overloading Operato
ID: 3723997 • Letter: O
Question
Objectives:
1. Understanding Operator Overloading in C++.
2. Overloading Operators as Member or Non-member functions.
3. Writing friend functions to overload the stream insertion and stream extraction operators.
4. Writing a simple function template to handle parameters of multiple types.
main function:
implementation file:
header:
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 box! 's length = 4, width = 5, and height 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 box! 's length-4, width = 5, and height-9 boxl+ 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 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
Note : I have to inclde some more functionality .I will update the code.thank u
___________________
BoxType.h
#ifndef BOXTYPE_H
#define BOXTYPE_H
class BoxType
{
ostream& operator<<(ostream& dout, const BoxType& b);
istream& operator>>(istream& din, BoxType& b);
public:
BoxType();
BoxType(double ,double,double);
void setLength(double);
void setWidth(double);
void setHeight(double);
BoxType operator+(const BoxType& b1,const BoxType& b2) const;
BoxType operator-(const BoxType& b1,const BoxType& b2) const;
BoxType operator/(const BoxType& b1,const BoxType& b2) const;
BoxType operator*(const BoxType& b1,const BoxType& b2) const;
bool operator<(const BoxType& b1,const BoxType& b2) const;
bool operator>(const BoxType& b1,const BoxType& b2) const;
bool operator==(const BoxType& b1,const BoxType& b2) const;
bool operator!=(const BoxType& b1,const BoxType& b2) const;
BoxType& operator++();
BoxType& operator--();
BoxType operator++(BoxType& b);
BoxType operator--(BoxType& b);
ostream& operator<<(ostream& dout, const BoxType& b);
istream& operator>>(istream& din, BoxType& b);
private:
double length;
double width;
double height;
};
#endif
_______________
BoxType.cpp
#include <iostream>
using namespace std;
#include "BoxType.h"
BoxType::BoxType()
{
}
BoxType::BoxType(double length,double width,double height)
{
this->length=length;
this->width=width;
this->height=height;
}
void BoxType::setLength(double length)
{
this->length=length;
}
void BoxType::setWidth(double width)
{
this->width=width;
}
void BoxType::setHeight(double height)
{
this->height=height;
}
BoxType BoxType::operator+(const BoxType& b1,const BoxType& b2) const
{
double l=b1.length+b2.length;
double w=b1.width+b2.width;
double h=b1.height+b2.height;
BoxType b(l,w,h);
return b;
}
BoxType BoxType::operator-(const BoxType& b1,const BoxType& b2) const
{
double l=b1.length-b2.length;
double w=b1.width-b2.width;
double h=b1.height-b2.height;
BoxType b(l,w,h);
return b;
}
BoxType BoxType::operator/(const BoxType& b1,const BoxType& b2) const
{
double l=b1.length/b2.length;
double w=b1.width/b2.width;
double h=b1.height/b2.height;
BoxType b(l,w,h);
return b;
}
BoxType BoxType::operator*(const BoxType& b1,const BoxType& b2) const
{
double l=b1.length*b2.length;
double w=b1.width*b2.width;
double h=b1.height*b2.height;
BoxType b(l,w,h);
return b;
}
bool BoxType::operator<(const BoxType& b1,const BoxType& b2) const
{
if(b1.length*b1.width*b1.height<b2.length*b2.width*b2.height)
return true;
else
return false;
}
bool BoxType::operator>(const BoxType& b1,const BoxType& b2) const
{
if(b1.length*b1.width*b1.height>b2.length*b2.width*b2.height)
return true;
else
return false;
}
bool BoxType::operator==(const BoxType& b1,const BoxType& b2) const
{
if(b1.length==b2.length && b1.width == b2.width && b3.height == b2.height)
return true;
else
return false;
}
bool BoxType::operator!=(const BoxType& b1,const BoxType& b2) const
{
if(b1.length==b2.length && b1.width == b2.width && b3.height == b2.height)
return false;
else
return true;
}
ostream& operator<<(ostream& dout, const BoxType& b)
{
dout<<"Length is "<<b.length<<". Width is "<<b.width<<". Height is "<<b.height<<"."<<endl;
return dout;
}
istream& operator>>(istream& din, BoxType& b)
{
din>>b.length;
din>>b.width;
din>>b.height;
return din;
}
// This function will compute increment the sides of the rectangle
void BoxType::operator++()
{
length++;
width++;
height++;
}
// This function will compute decrement the sides of the rectangle
void BoxType::operator--()
{
length--;
width--;
height--;
}
_________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.