Question: For lab 6 , you need to: Create a drawing application so the user can add different kinds of shapes to a canvas. All these

For lab 6, you need to:
Create a drawing application so the user can add different kinds of shapes to a canvas. All these shapes have a few things in common, like background color, border and so on.
As your exercise,
1. Create a new class. Shape and let two classes: Rectangle and Circle derive from the Shape class.
2. For each class you should implement the member functions in the implementation file.
3. Add a function to base class. The function is draw(). Let two derived classes override this function.
4. Back to the main file, create a function called showShape(). Pass a Shape object as the argument and draw shape for implementation.
5. Use Polymorphic Collection (I am not sure what this is). Create a vector of Shape, put a rectangle and a circle into it, loop over the vector, and draw a rectangle and a circle on the console. Use late or dynamic binding (which I also need help understanding).
6. Rewrite this code using unique pointer instead.
Here are the UMLs.
Shape UML:
-background: string
+getBackground()
+setBackground()
+void draw()
Rectangle UML:
-width: int
-heigth: int
+Rectangle()
+Rectangle(int width, int height)
+void draw()
+int getArea()
Circle UML:
-radius: float
+Circle()
+Circle(float radius)
+float getArea()
+void draw()
I want to try to get the following code to work.
vector > uniqueShapes;
//Since this is a vector of unique Shape pointers, it cannot use rectangle or circle pointers.
//I was accidentally trying to downcast a shape to a rectangle and a circle (which is illegal).
unique_ptr rectanglePtr = make_unique (4.0,5.0);
unique_ptr circlePtr = make_unique (6.0);
uniqueShapes.push_back(rectanglePtr);
uniqueShapes.push_back(circlePtr);
The four lines above are what is causing the problem. It says something about trying to access a deleted function.
uniqueShapes.push_back(make_unique (7.0,8.0));
uniqueShapes.push_back(make_unique (9.0));
for (int i =0; i < uniqueShapes.size(); i++)
{
showShape(shapes.at(i));
}
What am I doing wrong?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!