Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Following examples from Week 1 0 , pgs . 2 1 - 2 5 lecture notes create your own Geometry class and the two subclasses

Following examples from Week10, pgs.21-25 lecture notes create your own Geometry class
and the two subclasses Circle and Square. Reproduce the behavior shown on pg.25 of
Week 10 notes.
a) Create an array of Geometry pointers on the stack. The number of elements is six. The
syntax looks like
Geometry* Geom[6] ;
where Geom is the name of the pointer array. Note that since this array is created on the
stack you do not need to explicitly delete this pointer array, however, you will need to delete
the objects that each of the pointers point to (will be discussed later).
b) We will create a total of three circles and three squares and store them on the heap using the
Geom pointer array. They will be stored in the sequence [Circle, Square, Circle, Square,
Circle, Square]. The first Circle/Square pair will have diameter D=1. and edge length Edge
=1. The second pair will have D=2.0 and Edge=2.0 and the third pair will have D=3.0
and Edge =3.0. Each object will be created using the new operator, for example, for the
first circle we have
Geom[0]= new Circle(1.0);
Use a similar process to create the remaining Squares and Circles storing all six elements into
the Geom pointer array. In your main program be sure to delete the memory pointed to by
the Geom pointer before the end of the main program by using statements such as:
delete Geom[i];
where i=0,1,dots,5. This should be located right before the return 0 ; in main.
c) After you have created the 6 Geometry objects, use a for loop to iterate over all the Geom
pointers and calls the Show () function. Note if this is done correctly it will automatically
call the "Circle Show()" function for the first element of the array, the "Square Show()"
function for the second element, etc. The syntax for this is something like
Geom [i]->Show () ; for each objects
NOTES FROM WEEK :
# include < iostream >
using namespace std;
class Geometry
{ protected:
double Area;
double Perimeter;
public:
Geometry();
virtual ~Geometry();
virtual void Show();
};
Geometry::Geometry()
{ cout << endl;
cout << "Geometry Cnstr Called" << endl;
Area =0.;
Perimeter =0.;
}
Geometry::~Geometry()
{ cout << "Geometry Dstr Called" << endl;
}
void Geometry::Show()
{ cout << endl;
cout << "Area ="<< Area << endl;
cout << "Perimeter="<< Perimeter << endl;
cout << endl;
}
class Circle : public Geometry
{ protected:
double D; // Diameter
const double PI;
public:
Circle(double d=0.) : PI(3.141592654)
{ cout<<"Circle Cnstr Called"<

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_2

Step: 3

blur-text-image_3

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

What role do hormone levels play in mood?

Answered: 1 week ago