Question: Create a base class named Rectangle containing length and width data members (10 points). From this class, derive a class named Box with another data
Create a base class named Rectangle containing length and width data members (10 points). From this class, derive a class named Box with another data member named depth (10 points). The member functions of the base class Rectangle should consist of a constructor and an area() function (10 points). The derived class Box should have a constructor, a volume() function, and and override function named area() that returns the surface area of the box (10 points).
Include the classes in a working C++ program. Have your program call all the member functions in each class with regular objects (10 points) and dynamic objects (10 points), and make assignments from derived class to base class in both object types (10 points each type).
Create an array of pointers to the base class of size 4 (10 points). Create dynamic objects of both classes and assign them to elements in the array (10 points). Use a loop to display the area of each element of the array (20 points). Use another loop and display the volume of each element of the array if it is a Box (20 points)
Sample Code:
#include "Box.h" #include "Rectangle.h" using std::cout; using std::endl; using std::ios; using namespace SavitchSale; int main() { Rectangle r(10.0, 20.0); Box b(15.0, 25.0, 5.0); Rectangle *pR = new Rectangle(13.0, 23.0); Box *pB = new Box(14.0, 24.0, 4.0); cout << "Rectangle object:" << endl; cout << "length=" << r.getLength() << endl; cout << "width=" << r.getWidth() << endl; cout << "area=" << r.area() << endl; cout << "Rectangle pointer:" << endl; cout << "length=" << pR->getLength() << endl; cout << "width=" << pR->getWidth() << endl; cout << "area=" << pR->area() << endl; r.setLength(33.0); r.setWidth(43.0); pR->setLength(86.0); pR->setWidth(96.0); cout << "Box object:" << endl; cout << "length=" << b.getLength() << endl; cout << "width=" << b.getWidth() << endl; cout << "depth=" << b.getDepth() << endl; cout << "area=" << b.area() << endl; cout << "volume=" << b.volume() << endl; cout << "Box pointer:" << endl; cout << "length=" << pB->getLength() << endl; cout << "width=" << pB->getWidth() << endl; cout << "depth=" << pB->getDepth() << endl; cout << "area=" << pB->area() << endl; cout << "volume=" << pB->volume() << endl; b.setLength(99.0); b.setWidth(109.0); b.setDepth(89.0); pB->setLength(48.0); pB->setWidth(38.0); pB->setDepth(28.0); r = b; delete pR; pR = pB; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
