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

For this project, you will create a series of classes to represent some simple g

ID: 3547676 • Letter: F

Question

For this project, you will create a series of classes to represent some simple geometric shapes and a small program to test your classes.

Shape is an abstract base class.

Data Members

A Shape has the following private data member:

Methods

The Shape class should have the following public methods:

a constructor that takes a const string& argument and uses it to initialize the shape's color. Since the Shape class is abstract, this constructor will only be invoked by a derived-class constructor.

a virtual method called print() that takes no arguments and returns nothing. The method should print the color.

a pure virtual method called get_area() that takes no arguments and returns a double. Since it is pure virtual (or abstract), this method has no definition, only a prototype. It must be defined in any concrete class derived from Shape.

Circle is derived from Shape using public inheritance.

Data Members

A Circle has the following private data member:

Methods

The Circle class should have the following methods:

a constructor that takes a string to initialize the circle's color and an int to initiallize the circle's radius. The color string should be passed to the Shape constructor.

an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word "circle" followed by the circle's radius and area, e.g.:

an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the circle's area based on its radius.

Rectangle is derived from Shape using public inheritance.

Data Members

A Rectangle has the following private data member:

Methods

The Rectangle class should have the following methods:

a constructor that takes a string to initialize the circle's color and two ints to initialize the rectangle's height and width. The color string should be passed to the Shape constructor.

an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word "rectangle" followed by the rectangle's height, width, and area, e.g.:

an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the rectangle's area based on its height and width.

Triangle is derived from Shape using public inheritance.

Data Members

A Triangle has the following private data member:

Methods

The Triangle class should have the following methods:

a constructor that takes a string to initialize the triangle's color and two ints to initialize the triangle's height and base. The color string should be passed to the Shape constructor.

an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word "triangle" followed by the triangle's height, base, and area, e.g.:

an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the rectangle's area based on its height and width.

Write a test program that creates either an array or an STL vector of pointers to Shape objects.

Dynamically create some Circles, Rectangles, and Triangles (at least two of each). After creating each object, add it to the array or vector.

Loop through the array or vector of Shape pointers and call the print() method for each of them.

Loop through the array or vector of Shape pointers again and call the print() method for each of the Circle objects in the array or vector.

Loop through the list of Shape pointers one more time and delete each object.

Output from this program should look something like this:

Explanation / Answer

t is not always obvious how a class hierarchy should be developed. To illustrate this, consider the Circle class introduced in the previous chapter, which may be summarized briefly as follows (the details of the methods have been suppressed to save space):

This class treats a circle in a fairly abstract, geometric fashion. We chose to extend it to derive a class that had the property that the circle might be drawn on a screen or plotter:

and noted that a DrawableCircle is-a Circle still - we can compute its area, compare it with other circles, and so on. Since all classes are ultimately subclasses of the basic Object class, one of the methods defined in the Circle class - equals() - overloads a method in the Object class, and one - toString() - overrides a method in the Object class.

What we have done for circles we could do for other geometric shapes. Geometric shapes, incidentally, are a great favourite among authors of introductory books on object-oriented programming! It will suffice to illustrate just one more - we can define a class for Square and extend it to derive a subclass for DrawableSquare:

and doubtless you can see that you could do much the same for other shapes, with considerable increase in internal complexity for some of these, perhaps.

We can depict the class hierarchy for these classes on a graph:

Nobody would ever say that a Circle is-a Square, even though the classes export rather similar methods. But one might observe that circles and squares are both shapes - and indeed are shapes that one might want to draw - and that this commonality might suggest that we insert a different class into the top end of the hierarchy, corresponding to the rather abstract idea of a general shape located at coordinates (x, y).

We might now derive a subclass of the Shape class to define a Circle:

with an analogous class for Square and, indeed, for any other shape you might care to want to draw - and all these classes would have the commonality of defining an appropriate draw() method. Our class hierarchy might then look like this:

There are a few subtleties about this that we should hasten to point out:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote