Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/////////////////////////////////////////////////////////////////////// // // //

ID: 3639579 • Letter: #

Question

///////////////////////////////////////////////////////////////////////
// //
// This program 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)
{} // everything 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 ///////////


//////////////////// 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 //
////////////////////////////////////////////////////////////////////////

//ADD CODE FOR THE CIRCLE CLASS HERE


///////////// 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 3, 4 and 5
//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 TRIANGLE 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;
}

Explanation / Answer

#include 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