Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Redo Programming Exercise 3 by overloading the operators as nonmembers of the class boxType Write a program that tests the operations of the class boxType

Redo Programming Exercise 3 by overloading the operators as nonmembers of the class boxType Write a program that tests the operations of the class boxType ********************************* boxType.h ********************************* #ifndef boxType_H #define boxType_H #include "rectangleType.h" class boxType: public rectangleType { //Overload the stream insertion and extraction operators friend ostream& operator<<(ostream&, const boxType &); friend istream& operator>>(istream&, boxType &); public: void setDimension(double l, double w, double h); //Function to set the length, width, and height //of the box. //Postcondition: length = l; width = w; height = h; double getHeight() const; //Function to return the height of the box. //Postcondition: The value of height is returned. double area() const; //Function to return the surface area of the box. //Postcondition: The surface area of the box is // calculated and returned. double volume() const; //Function to return the volume of the box. //Postcondition: The volume of the box is // calculated and returned. boxType(); //Default constructor //Postcondition: length = 0; width = 0; height = 0; boxType(double l, double w, double h); //Constructor with parameters //Postcondition: length = l; width = w; height = h; //Overload the arithmetic operators boxType operator + (const boxType &) const; boxType operator - (const boxType &) const; boxType operator * (const boxType&) const; //Overload the increment and decrement operators boxType operator ++ (); //pre-increment boxType operator ++ (int); //post-increment boxType operator -- (); //pre-decrement boxType operator -- (int); //post-decrement //Overload the relational operators bool operator == (const boxType&) const; bool operator != (const boxType&) const; bool operator <= (const boxType&) const; bool operator < (const boxType&) const; bool operator >= (const boxType&) const; bool operator > (const boxType&) const; protected: double height; }; #endif ************************* boxType.cpp ************************* #include #include #include "rectangleType.h" #include "boxType.h" using namespace std; void boxType::setDimension(double l, double w, double h) { rectangleType::setDimension(l, w); if (h >= 0) height = h; else height = 0; } double boxType::getHeight() const { return height; } double boxType::area() const { return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); } double boxType::volume() const { return rectangleType::area() * height; } boxType::boxType() { height = 0.0; } boxType::boxType(double l, double w, double h) : rectangleType(l, w) { if (h >= 0) height = h; else height = 0; } boxType boxType::operator + (const boxType & box) const { boxType tempBox; tempBox.length = length + box.length; tempBox.width = width + box.width; tempBox.height = height + box.height; return tempBox; } boxType boxType::operator - (const boxType & box) const { boxType tempBox; assert(length >= box.length && width >= box.width && height >= box.height); tempBox.length = length + box.length; tempBox.width = width + box.width; tempBox.height = height + box.height; return tempBox; } boxType boxType::operator * (const boxType & box) const { boxType tempBox; tempBox.length = length * box.length; tempBox.width = width * box.width; tempBox.height = height * box.height; return tempBox; } boxType boxType::operator ++ () { ++length; ++width; ++height; return *this; //return the incremented value of the object } boxType boxType::operator ++ (int u) { boxType temp = *this; //use this pointer to copy //the value of the object length++; width++; height; return temp; //return the old value of the object } boxType boxType::operator--() { //Decrement the length and width assert(length >= 1 && width >= 1 && height >= 1); --length; --width; --height; return *this; //return the incremented value of the object } boxType boxType::operator -- (int u) { boxType temp = *this; //use this pointer to copy //the value of the object //Decrement the length and width assert(length >= 1 && width >= 1 && height >= 1); length--; width--; height--; return temp; //return the old value of the object } bool boxType::operator == (const boxType& box) const { return (volume() == box.volume()); } bool boxType::operator != (const boxType& box) const { return (volume() != box.volume()); } bool boxType::operator <= (const boxType& box) const { return (volume() <= box.volume()); } bool boxType::operator < (const boxType& box) const { return (volume() < box.volume()); } bool boxType::operator >= (const boxType& box) const { return (volume() >= box.volume()); } bool boxType::operator > (const boxType& box) const { return (volume() > box.volume()); } ostream& operator<<(ostream& osObject, const boxType& box) { osObject << "Length = " << box.length << "; Width = " << box.width << "; Height = " << box.height; return osObject; } istream& operator>>(istream& isObject, boxType& box) { isObject >> box.length >> box.width >> box.height; return isObject; } ******************* rectangleType.h ******************* #ifndef H_rectangleType #define H_rectangleType #include using namespace std; 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 ************************** rectangleType.cpp ************************** #include #include #include "rectangleType.h" using namespace std; 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

6. Use short-range goals to assist in achieving long-range goals.

Answered: 1 week ago