Question
(C++)Create a new derived class treasureChest from boxType. Start with the baseline code for rectangleType and boxType, and add the following to boxType: Create an
(C++)Create a new derived class treasureChest from boxType. Start with the baseline code for rectangleType and boxType, and add the following to boxType:
Create an enumerated type for color valid colors are green (green = 0), black (black = 1), brown (brown = 2), and white (white = 3). (Note: you can choose other, appropriate values for your colors.)
Add a boxColor as a private member variable to boxType along with a setBoxColor mutator and getBoxColor accessor.
Modify the parameterized constructor for boxType to take a parameter to set boxColor. The default constructor should set boxColor to green.
Next, derive treasureChest from boxType using public inheritance. treasureChest has the following properties:
Note: this description is deliberately not a UML class diagram or similar. You should sketch an outline in pseudocode for your new class as you read these descriptions (and the client) to ensure you meet all the requirements. (You dont have to turn your sketch in.)
A treasureChest can store treasure! Treasure in this case will be represented by a static array of 100 coins. Use an enumerated type to represent a coin, with the following three types and values: gold (value 25) silver (value 10) copper (value 1) nada (value 0)
A treasureChest has a current total number of coins that are stored. Keep track of this with a private member variable of appropriate type. The default is 0, i.e., the chest can hold 100 coins but starts empty (all elements in array are set to nada). Create an appropriate accessor to return the total number of coins currently in the chest.
Treasure can be added to the chest via a class method that takes a coin denomination and places it in the next available slot in the array of coins (i.e., the next slot with nada).
Treasure can be removed from the chest one piece at a time with a method that takes a coin as input parameter and removes one coin of that type from the array (if any of that type are present).
The color of the treasureChest is restricted to black or brown: The treasureChest constructors should set boxColor appropriately (recall the default box color is green).
Add a method called getSum to return the total value of all the coins in the chest.
test your solution with an unmodified copy of the client.
Below are the baseline code for rectangleType and boxType and the test client.
//rectangleType.h #ifndef rectangleType_H #define rectangleType_H
class rectangleType { public: //Function to set the length and width of the rectangle. //Postcondition: length = l; width = w; void setDimension(double l, double w); //Function to return the length of the rectangle. //Postcondition: The value of length is returned. double getLength() const; //Function to return the width of the rectangle. //Postcondition: The value of width is returned. double getWidth() const;
//Function to return the area of the rectangle. //Postcondition: The area of the rectangle is // calculated and returned. double area() const; //Function to return the perimeter of the rectangle. //Postcondition: The perimeter of the rectangle is // calculated and returned. double perimeter() const; //Function to output the length and width of //the rectangle. void print() const; //Default constructor //Postcondition: length = 0; width = 0; rectangleType(); //Constructor with parameters //Postcondition: length = l; width = w; rectangleType(double l, double w);
private: double length; double width; };
#endif
//rectangleType.cpp #include
rectangleType::rectangleType(double l, double w) { setDimension(l, w); }
rectangleType::rectangleType() { length = 0; width = 0; }
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); }
void rectangleType::print() const { std::cout << "Length = " << length << "; Width = " << width; }
//boxType.cpp #include
boxType::boxType() { height = 0.0; }
boxType::boxType(double l, double w, double h) : rectangleType(l, w) { if (h >= 0) height = h; else height = 0; }
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 getLength()*getWidth()*height; }
void boxType::print() const { rectangleType::print(); std::cout << "; Height = " << height; }
//boxType.h #ifndef boxType_H #define boxType_H
#include "rectangleType.h" #include
class boxType : private rectangleType { public: //Function to set the length, width, and height //of the box. //Postcondition: length = l; width = w; height = h; void setDimension(double l, double w, double h);
//Function to return the height of the box. //Postcondition: The value of height is returned. double getHeight() const; //Function to return the surface area of the box. //Postcondition: The surface area of the box is // calculated and returned. double area() const; //Function to return the volume of the box. //Postcondition: The volume of the box is // calculated and returned. double volume() const; //Function to output the length, width, and height of a box. void print() const; //Default constructor //Postcondition: length = 0; width = 0; height = 0; boxType(); //Constructor with parameters //Postcondition: length = l; width = w; height = h; boxType(double l, double w, double h);
private: double height; };
#endif
//Test client.cpp #include
using namespace std;
void printBoxColor(boxType *box);
int main() { cout << "========== Testing boxType ==========" << endl;
//test to make sure boxType works with patterns now boxType *wcc = new boxType(1, 1, 1, white);
cout << "Box dimensions: "; wcc->print(); cout << endl; cout << "What color is it?" << endl; printBoxColor(wcc);
cout << endl << "========== Be ye testing of yon treasureChest ==========" << endl;
treasureChest *swag = new treasureChest(10, 10, 10, brown); //... and containing some coins swag->addCoin(gold); swag->addCoin(silver); swag->addCoin(gold); swag->addCoin(copper); swag->addCoin(silver); swag->addCoin(gold); swag->addCoin(copper);
//pirate tells us what color the chest is printBoxColor(swag); cout << endl;
//pirate removes some coins swag->removeCoin(gold); swag->removeCoin(silver); swag->removeCoin(copper);
//pirate tells us what's in the chest cout << "Arr matey, the chest contains " << swag->getNumCoins(); cout << " coins which, in total, is worth " << endl; cout << swag->getSum() << " dubloons "; cout << "(on my honor, I found it this way!)" << endl; cout << endl;
//pirate tells us color of chest after painting it cout << "Painting the chest black, matey..." << endl; swag->setBoxColor(black); printBoxColor(swag); cout << endl;
//pirate refuses to let us paint the chest green cout << endl << "No silly green pirate chests!" << endl; swag->setBoxColor(green); printBoxColor(swag); cout << endl;
//pirate tries to stuff chest with 101 more gold coins (it can only fit 100) for (int i = 0; i < 101; i++) swag->addCoin(gold); cout << endl << "Now there be " << swag->getNumCoins() << " coins in the chest." << endl;
cout << "Arr matey, the chest contains this much loot "; cout << "(hands off!): "; cout << swag->getSum() << " dubloons" << endl; cout << endl;
return 0; }
//This function prints the pattern of a boxType object //Parameters: pointer to a boxType object //Returns: nothing void printBoxColor(boxType *box) { color c = box->getBoxColor(); switch (c) { case green: cout << "The box is green!" << endl; break; case brown: cout << "The box is brown." << endl; break; case black: cout << "The box is black." << endl; break; case white: cout << "The box is white." << endl; break; default: cout << "Here be dragons!" << endl; } }
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