Question
C++ Chapter 11 defined the class boxType by extending the definition of the class rectangleType. In this exercise, derive the class boxType from the class
C++ Chapter 11 defined the class boxType by extending the definition of the class rectangleType. In this exercise, derive the class boxType from the class rectangleType, defined in Exercise 1, add the functions to overload the operators +,, *, ==, !=, <=, <, >=, >, and pre- and post-increment and decrement operators as members of the class boxType. Also overload the operators << and >>. Overload the relational operators by considering the volume of the boxes. For example, two boxes are the same if they have the same volume.
Write the definitions of the functions of the class boxType as defined above and then write a test program that tests various operations on the class
Here is my code Must use this existing code and modify. Must also use boxtype.cpp and boxtype.h:
--Main.cpp
#include
#include "rectangleType.h" using namespace std;
int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4;
cout << "rectangle1: " << rectangle1 << endl;
cout << "rectangle2: " << rectangle2 << endl;
rectangle3 = rectangle1 + rectangle2; cout << "rectangle3: " << rectangle3 << endl;
rectangle4 = rectangle1 * rectangle2; cout << "rectangle4: " << rectangle4 << endl;
if (rectangle1 > rectangle2) cout << "Area of rectangle1 is greater than the area " << "of rectangle2 ." << endl; else cout << "Area of rectangle1 is less than or equal to the area " << "of rectangle2 ." << endl;
rectangle1++;
cout << "After increment the length and width of " << "rectangle1 by one unit, rectangle1: " << rectangle1 << endl;
rectangle4 = ++rectangle3;
cout << "New dimension of rectangle3: " << rectangle3 << endl; cout << "New dimension of rectangle4: " << rectangle4 << endl;
return 0; }
-- rectangletype.cpp
#include
void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length = 0;
if (w >= 0) width = w; else width = 0; }
double rectangleType::getLength() const { return length; }
double rectangleType::getWidth()const { return width; }
double rectangleType::area() const { return length * width; }
double rectangleType::perimeter() const { return 2 * (length + width); }
rectangleType::rectangleType(double l, double w) { setDimension(l, w); }
rectangleType::rectangleType() { length = 0; width = 0; }
rectangleType rectangleType::operator++() { //increment the length and width ++length; ++width;
return *this; //return the incremented value of the object }
rectangleType rectangleType::operator++(int u) { rectangleType temp = *this; //use this pointer to copy //the value of the object //increment the length and width length++; width++;
return temp; //return the old value of the object }
rectangleType rectangleType::operator--() { //Decrement the length and width assert(length != 0 && width != 0); --length; --width;
return *this; //return the incremented value of the object }
rectangleType rectangleType::operator--(int u) { rectangleType temp = *this; //use this pointer to copy //the value of the object
//Decrement the length and width assert(length != 0 && width != 0); length--; width--;
return temp; //return the old value of the object }
rectangleType rectangleType::operator+ (const rectangleType& rectangle) const { rectangleType tempRect;
tempRect.length = length + rectangle.length; tempRect.width = width + rectangle.width;
return tempRect; }
rectangleType rectangleType::operator- (const rectangleType& rectangle) const { rectangleType tempRect;
assert(length >= rectangle.length && width >= rectangle.width);
tempRect.length = length - rectangle.length; tempRect.width = width - rectangle.width;
return tempRect; }
rectangleType rectangleType::operator*(const rectangleType& rectangle) const { rectangleType tempRect;
tempRect.length = length * rectangle.length; tempRect.width = width * rectangle.width;
return tempRect; }
bool rectangleType::operator== (const rectangleType& rectangle) const { return (area() == rectangle.area()); }
bool rectangleType::operator!= (const rectangleType& rectangle) const { return (area() != rectangle.area()); }
bool rectangleType::operator<= (const rectangleType& rectangle) const { return (area() <= rectangle.area()); }
bool rectangleType::operator< (const rectangleType& rectangle) const { return (area() < rectangle.area()); }
bool rectangleType::operator>= (const rectangleType& rectangle) const { return (area() >= rectangle.area()); }
bool rectangleType::operator> (const rectangleType& rectangle) const { return (area() > rectangle.area()); }
ostream& operator<<(ostream& osObject, const rectangleType& rectangle) { osObject << "Length = " << rectangle.length << "; Width = " << rectangle.width;
return osObject; }
istream& operator>>(istream& isObject, rectangleType& rectangle) { isObject >> rectangle.length >> rectangle.width;
return isObject; }
-- rectangletype.h
#ifndef H_rectangleType #define H_rectangleType #include
class rectangleType { //Overload the stream insertion and extraction operators friend ostream& operator<<(ostream&, const rectangleType &); friend istream& operator>>(istream&, rectangleType &);
public: void setDimension(double l, double w); double getLength() const; double getWidth() const; double area() const; double perimeter() const;
//Overload the arithmetic operators rectangleType operator + (const rectangleType &) const; rectangleType operator - (const rectangleType &) const; rectangleType operator * (const rectangleType&) const;
//Overload the increment and decrement operators rectangleType operator ++ (); //pre-increment rectangleType operator ++ (int); //post-increment rectangleType operator -- (); //pre-decrement rectangleType operator -- (int); //post-decrement
//Overload the relational operators bool operator == (const rectangleType&) const; bool operator != (const rectangleType&) const; bool operator <= (const rectangleType&) const; bool operator < (const rectangleType&) const; bool operator >= (const rectangleType&) const; bool operator > (const rectangleType&) const;
//constructors rectangleType(); rectangleType(double l, double w);
protected: double length; double width; };
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started