C++ with Inheritance Pproblem involving inheritance. You have to finish completing code that creates a class called shape, from which 3 classes are derived that are called square and triangle. I am giving you the code for the shape class and the square class. Your job is to add code for a triangle class and modify the statements that call and print out the values at the end of the program. A TEMPLATE file has been provided that provides much of the code. The code that needs to be added is highlighted in red. HINTS file provide a good plan on how to do the triangle code. The following is the data for the objects: Shape id = 100 Square id = 200, side length = 4 Triangle id = 300, sides are 1, 2, and 4 As a challenge, add the code for the Circle code... a few hints are provided in the previously mentioned HINTS file. Circle id = 400, radius = 1 Template to use for Assignment 4 Use the template below and fill in the areas in Red to complete it. /////////////////////////////////////////////////////////////////////// // Homework: // // Homework description: This homework will demonstrate how we // // can use inheritance to extend on a simple class // // We will define a class called shape that will support holding // // a shape ID (an integer) and a virtual public function that can // // be called to print out it's ID and perimeter (sum of its sides) // // // // From the shape, we will derive classes that represent // // different kinds of shapes. Each of these classes can print out // // its perimeter. // // // // The test main will make objects of both shape and the derived // // types and will print out the id and perimeter of each object // // // // The class shape is given, together with a shape called a square // // The assignment is to add two more shapes, called a triangle // // and a circle, modify the test main to create objects of these // // types and print out the information for all of the shapes in // // the array of shapes. // ///////////////////////////////////////////////////////////////////////
#include <iostream> using namespace std; // class shape declaration follows. It contains a private integer // // to hold the shape ID, a constructor that receives the ID and saves// // it, a public function that returns the ID, and a virtual function // // called show_data that will print out the ID and the perimeter of // // the shape. class shape private: int id; // the shape id number // public: shape (int theid); // the constructor must get an id // int get_id(); // a function to retrieve id // virtual void show_data(); // a function that can be overridden in derived classes // ; // The shape class member definitions follow: shape::shape (int theid) : id(theid) // nothing to do since id has been saved // int shape::get_id() // get_id returns the id for the object // return id; // return the id number // void shape::show_data() // show_data will retrieve the id and print a msg // int ID = get_id(); // get the id value // cout << "object " << ID << " is a shape that has no perimeter!" << endl; //////////////////////////// this ends class shape ///////////////////// //////////////////// derived class square follows ////////////////////// // A square has 4 sides of equal length, so we need to only have // // a single private data item holding the side length. // // It will need a square constructor will has two parameters // // - the id of the square. It will pass the id during // // construction back to the shape part of the class // // - the length of a square side (a float) // // It will need a public member function called perimeter that will // // compute the perimeter of a square and return it to the caller // // It will need a show_data public member function to print out the // // square id and a computed perimeter of the square // //////////////////////////////////////////////////////////////////////// class square : public shape private: float sidelength; // the length of a side // public: square (int theid, float thesidelength); // the square constructor //
float perimeter(); // computes and returns the perimeter of a square // void show_data(); // prints the id and perimeter // ; ///// the square class member functions follow: // square::square(int theid, float thesidelength) : sidelength(thesidelength), shape(theid) // verything initialized, done // float square::perimeter() // the perimeter of a square is 4 * a side length return 4 * sidelength; // return the perimeter // void square::show_data() // retrieve the square ID and printout the id and perimeter // since we inherited the public member functions of shape, // we can call get_id to get the shape it for the square cout << "object " << get_id() << " is a square with a side of " << sidelength << " and a perimeter of " << perimeter() << endl; ///////////// end of derived class square /////////// // HINT: the perimeter of a triangle is the sum of its sides // // the perimeter of a circle is 2 * 3.14159 * radius // //////////////////// derived class triangle follows //////////////////// // A triangle has 3 sides, so we need to save the 3 side lengths // // It will need a triangle constructor will has 4 parameters // // - the id of the triangle. It will pass the id during // // construction back to the shape part of the class // // - the 3 sides of the triangle (use floats) // // It will need a public member function called perimeter that will // // compute the perimeter of a triangle and return it to the caller // // It will need a show_data public member function to print out the // // triangle id and a computed perimeter of the triangle // //////////////////////////////////////////////////////////////////////// //ADD CODE FOR THE TRIANGLE CLASS HERE ///// the triangle class member functions follow: // ///////////// end of derived class triangle /////////// //ADD CODE FOR THE CIRCLE CLASS HERE //////////////////// derived class circle follows ////////////////////// // A circle has a radius, so we need to only have // // a single private data item holding the radius. // // It will need a circle constructor will has two parameters //
// - the id of the circle. It will pass the id during // // construction back to the shape part of the class // // - the radius (use a float) // // It will need a public member function called perimeter that will // // compute the perimeter of a circle and return it to the caller // // It will need a show_data public member function to print out the // // circle id and a computed perimeter of the circle // //////////////////////////////////////////////////////////////////////// ///////////// end of derived class circle /////////// // The test main follows, performing the following steps: // 1. It makes a shape object // 2. It makes a square object // 3. It makes a triangle object // 4. It makes a circle object // 5. It makes an array of pointer to shape objects, placing the 4 objects in the array // 6. It loops through the array, printing out the information int main() //UPDATE THE NUMBER OF SHAPES WHEN YOU ADD THE CIRCLE AND TRIANGLE OBJECTS #define NUMSHAPES 2 // step 1. Make a shape object with id of 100 shape ashape (100); // step 2. Make a square object with id 200 and a side of 5 square asquare (200, 5); // step 3. Make a triangle object with id 300 and sides 1,2 and 4 //ADD CODE TO MAKE A TRIANGLE HERE // step 4. Make a circle object with radius 10 and id 400 //ADD CODE TO MAKE A CIRCLE HERE // step 5. Make an array to hold pointers to the objects, placing the // placing the object pointers in the array //ADD CODE TO ADD THE TRAINGLE AND CIRCLE OBJECT POINTERS HERE shape *shapearray[numshapes] = &ashape, &asquare; // step 6. Loop through the array, printing out the information for (int loop = 0; loop < NUMSHAPES; loop++) shapearray[loop]->show_data(); // all done, return return 0;
Hints Remember that a triangle consists of three sides, and each could be different sizes, so you'll need three private variables names to represent each side. This is done in the private area below for the triangle class. The best way to do this is to review how the Square was done since that code already exists. To help you out more, here is a template for the triangle code. The items in red indicate what you need to code. You don't have to use the code below, but feel free to use it if it helps. Triangle Code 1) UPDATE THE CLASS and its CODE class triangle : public shape private: float ALength; // the length of the first side // float BLength; // the length of the second side // float CLength; // the length of the third side // public: ; // add code to define the triangle constructor // // define a variable to store the perimeter // // define show data to print the id and perimeter // ///// the triangle class member functions follow: // triangle::triangle(int theid, float SideALength, float SideBLength, float SideCLength) : CLength(SideCLength), BLength(SideBLength), ALength(SideALength), shape(theid) // everything initialized, done // float triangle::perimeter() // the perimeter of a triangle is the sum of it's sides // use the private variables in the triangle class (ALenghth, BLenghth, and CLength) // to add the three sides together and return the value. You'll need a return statement here.
void triangle::show_data() // retrieve the triangle ID and printout the id and perimeter // since we inherited the public member functions of shape, // we can call get_id to get the shape it for the square // use cout to print the object, the three sides, and the parameter... just like we did with Square 2) Update the calls to make the Triangle code work // Update the constant to 3 now... since we added a triangle object #define NUMSHAPES 3 // step 3. Make a triangle object with id 300 and sides 1,2 and 3 triangle atriangle (300, 1, 2, 3); // step 4. Make a circle object with radius 10 and id 400 // Just leave this blank for now... until we add the circle code later // step 5. Make an array to hold pointers to the objects, placing the // placing the object pointers in the array shape *shapearray[numshapes] = &ashape, &asquare, &atriangle; The Circle Code Once you get the triangle code working, then add the code for the Circle object. Use what you learned with the triangle. Then update the code at the end to call it since we now have to process a new Circle object. //UPDATE THE NUMBER OF SHAPES to 4 when you add the circle object #define NUMSHAPES 4 // step 4. Make a circle object with radius 10 and id 400 circle acircle (400, 10); // step 5. Add the Circle to the shapearray shape *shapearray[numshapes] = &ashape, &asquare, &atriangle, âle;