Purpose: This is a continuation of assignment #2 that uses polymorphic inheritance and also inheritance from part of the C++ Standard Library with the vector template sequence container class. You are to write the code in Visual Studio 2013 for a C++ language console application that holds the data of a Vector Graphics application (there is no actual graphics in the assignment) using dynamic memory allocation for its data. Note that some of the overloaded operators from assignment 2 have been removed in order to allow you to focus on the new features.

In this assignment in addition to Lines there are now Rectangles and Ellipses, all derived from the abstract base class Shape. Pointers of instances of these that belong to a GraphicElement are saved in a vector of Shape pointers that is the GraphicElement. Polymorphism has been added in the form of two virtual functions (CalculateCentre() and Report()) of class Shape. This means that instances of Line, Rectangle and Ellipse can have polymorphic functions called with these Shape* pointers and their own overridden versions of the functions will correctly execute. So, for example, to Report() for an Ellipse its Shape* pointer simply does ->Report() and polymorphism causes the Ellipse version to execute your code makes no attempt to discover that it actually is an Ellipse*. Polymorphism makes sure the right function is used for a particular Shape.

Specifically, your code in ostream& operator<<(ostream& os, GraphicElement& RGE) to report the Shapes must be like this:

for (GraphicElement::iterator i = RGE.begin(); i != RGE.end(); i++)
(*i)->Report();

and the code to call CalculateCentre() (for example to add up all the Shape centres) must be like this:

for(GraphicElement::iterator i = RGE.begin(); i != RGE.end(); i++)
centre = centre + (*i)->CalculateCentre();

The inheritance hierarchy is: see image.

The three different Shapes (Line, Rectangle and Ellipse) treat their p1 and p2 parameters differently and calculate their centres according to the following formulas.

Shape p1= p2 = Centre =
Line start coords end coords (p1 + p2)/2
Rectangle top left coords bottom right coords (p1 + p2)/2
Ellipse centre coords width = p2.x, height = p2.y p1

The centre of a GraphicElement that is reported in the output is simply the average of the centres of all its Shapes.

Part of the code (minus the usual headers) is shown later. You MUST use this code without modification (not a single character changed): no code added or removed (except the usual headers in source code files), no macros, no defines and no statics. Your task is to implement the member functions of Pair, Line, Rectangle, Ellipse, GraphicElement and VectorGraphic and not add any new ones - your code is in the six source code files Pair.cpp, Line.cpp, Rectangle.cpp, Ellipse.cpp, GraphicElement.cpp and VectorGraphic.cpp that you will submit.

In this assignment, when the application is running the user can

  • Add a new GraphicElement (there is no limit to the number) to the VectorGraphic sequence container,
  • Delete a Graphic Element from the VectorGraphic,
  • List the details of all the GraphicElements in the VectorGraphic.

An example of the output of the running application is given at the end. Yours must work identically and produce identical output.

Note the following:

  • There is never any unfilled allocated dynamic memory vectors expand and contract dynamic memory as needed,
  • You must use C++ constructs and classes. You must only use new and delete for dynamic memory management and cin and cout for input and output,
  • Use functions like strlen() and strcpy() or similar etc. from the standard C library to handle strings
  • When the application terminates there is no residual dynamically allocated memory so it does not have a resource leak (or you lose 30%),
  • .cpp source code files will include appropriate headers in the order that the compiler needs to read them.

Example code I will use it to mark your assignment. Dont change or add to it (not even a single character). This code is also in a text file on BlackBoard, so you dont need to type it:

class Pair
{
double x, y;
public:
Pair():x(0),y(0){}
Pair(double x, double y):x(x), y(y){}
Pair operator+(Pair&);
Pair operator/(double);
void Report();
};
// abstract base class
class Shape
{
protected:
Pair p1;
Pair p2;
char name[256];
public:
Shape(char* name, Pair p1, Pair p2):p1(p1),p2(p2)
{
strcpy_s(this->name, name);
};
virtual ~Shape(){}
virtual Pair CalculateCentre() = 0;// pure virtual with no body
virtual void Report() = 0; // pure virtual with no body
};
class Line:public Shape // is-a Shape
{
public:
Line(char* name, Pair start, Pair end) :Shape(name, start, end){};
~Line(){}
Pair CalculateCentre();
void Report();
};
class Rectangle:public Shape // is-a Shape
{
public:
Rectangle(char* name, Pair topLeft, Pair bottomRight) :Shape(name, topLeft, bottomRight){};
~Rectangle(){}
Pair CalculateCentre();
void Report();
};
class Ellipse:public Shape // is-a Shape
{
public:
Ellipse::Ellipse(char* name, Pair centre, Pair axes) :Shape(name,centre, axes){}
~Ellipse(){}
Pair CalculateCentre();
void Report();
};
class GraphicElement: public vector // is-a vector
{
static const int SIZE = 256;
char name[SIZE];
public:
GraphicElement(Shape**, char*, unsigned int );
GraphicElement(const GraphicElement&);
GraphicElement& operator=(GraphicElement&);
~GraphicElement();
friend ostream& operator<<(ostream&, GraphicElement&);
};
class VectorGraphic: public vector // is-a vector
{
public:
VectorGraphic();
~VectorGraphic();
void AddGraphicElement();
void DeleteGraphicElement();
friend ostream& operator<<(ostream&, VectorGraphic&);
};
// ass3.cpp – add headers

enum{ RUNNING = 1 };

int main()
{
char response;
VectorGraphic Image;

while (RUNNING)
{
cout << endl << "Please select an option:" << endl;
cout << "1. Add a Graphic Element" << endl;
cout << "2. Delete a GraphicElement" << endl;
cout << "3. List all the Graphic Elements" << endl;
cout << "q. Quit" << endl;
cout << "CHOICE: ";
cin >> response;
switch (response)
{
case '1':Image.AddGraphicElement(); break;
case '2':Image.DeleteGraphicElement(); break;
case '3':cout< case 'q': return 0;
default:cout << "Please enter a valid optionn";
}
cout << endl;
}
}

EXAMPLE OUTPUT

Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE: 1
Adding a Graphic Element
Please enter the name of the new Graphic Element (<256 characters): face
Please enter the number of Shapes it contains: 4
Please enter the type (L for line, R for rectangle, E for ellipse) for Shape #0
L
Please enter the name of the new line(<256 characters): mouth
please enter the coordinates of the start point: (x,y) -1 -1
please enter the coordinates of the end point: (x,y) 1 -1
Please enter the type (L for line, R for rectangle, E for ellipse) for Shape #1
R
Please enter the name of the new Rectangle(<256 characters): nose
please enter the coordinates of the top-left: (x,y) -.25 2
please enter the coordinates of the bottom-right: (x,y) .25 0
Please enter the type (L for line, R for rectangle, E for ellipse) for Shape #2
E
Please enter the name of the new Ellipse(<256 characters): left eye
please enter the coordinates of the centre: (x,y) -1 3
please enter the width and height: (width, height) .5 .25
Please enter the type (L for line, R for rectangle, E for ellipse) for Shape #3
E
Please enter the name of the new Ellipse(<256 characters): right eye
please enter the coordinates of the centre: (x,y) 1 3
please enter the width and height: (width, height) .5 .25


Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE: 3
VectorGraphic Report

Reporting Graphic Element 0
Graphic Element face
The centre = x = 0; y = 1.5
List of Shapes in face
Shape LINE mouth
start coordinates: x = -1; y = -1
end coordinates: x = 1; y = -1
Shape RECTANGLE nose
top left coordinates: x = -0.25; y = 2
bottom right coordinates: x = 0.25; y = 0
Shape ELLIPSE left eye
centre coordinates: x = -1; y = 3
axes dimensions: x = 0.5; y = 0.25
Shape ELLIPSE right eye
centre coordinates: x = 1; y = 3
axes dimensions: x = 0.5; y = 0.25



Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE: 1
Adding a Graphic Element
Please enter the name of the new Graphic Element (<256 characters): hat
Please enter the number of Shapes it contains: 2
Please enter the type (L for line, R for rectangle, E for ellipse) for Shape #0
L
Please enter the name of the new line(<256 characters): rim
please enter the coordinates of the start point: (x,y) -4 5
please enter the coordinates of the end point: (x,y) 4 5
Please enter the type (L for line, R for rectangle, E for ellipse) for Shape #1
R
Please enter the name of the new Rectangle(<256 characters): crown
please enter the coordinates of the top-left: (x,y) -3 7
please enter the coordinates of the bottom-right: (x,y) 3 5


Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE: 3
VectorGraphic Report

Reporting Graphic Element 0
Graphic Element face
The centre = x = 0; y = 1.5
List of Shapes in face
Shape LINE mouth
start coordinates: x = -1; y = -1
end coordinates: x = 1; y = -1
Shape RECTANGLE nose
top left coordinates: x = -0.25; y = 2
bottom right coordinates: x = 0.25; y = 0
Shape ELLIPSE left eye
centre coordinates: x = -1; y = 3
axes dimensions: x = 0.5; y = 0.25
Shape ELLIPSE right eye
centre coordinates: x = 1; y = 3
axes dimensions: x = 0.5; y = 0.25

Reporting Graphic Element 1
Graphic Element hat
The centre = x = 0; y = 5.5
List of Shapes in hat
Shape LINE rim
start coordinates: x = -4; y = 5
end coordinates: x = 4; y = 5
Shape RECTANGLE crown
top left coordinates: x = -3; y = 7
bottom right coordinates: x = 3; y = 5



Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE: 2
Deleting a Graphic Element
Please enter the index to delete in the range 0 to 1
0
erase index = 0


Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE: 3
VectorGraphic Report

Reporting Graphic Element 0
Graphic Element hat
The centre = x = 0; y = 5.5
List of Shapes in hat
Shape LINE rim
start coordinates: x = -4; y = 5
end coordinates: x = 4; y = 5
Shape RECTANGLE crown
top left coordinates: x = -3; y = 7
bottom right coordinates: x = 3; y = 5



Please select an option:
1. Add a Graphic Element
2. Delete a GraphicElement
3. List all the Graphic Elements
q. Quit
CHOICE:
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.