In this project, you are to develop the core library for a Geometric modeling pr
ID: 3770715 • Letter: I
Question
In this project, you are to develop the core library for a Geometric modeling
project. You will use object-oriented design principles to develop a set of
classes that use virtual methods, multiple inheritance and exceptions. Your
requirement for this project is to write C++ code to do the following:
1. Write C++ class Circle. (See attached UML)
Circle is a Shape representing a standard circle, with an x- and y-coordinate
for its center and a radius.
1. Circle should be a subclass of the provided Shape class. It should contain
the following attributes (private visibility)
1. x: double - x-coordinate of its center
2. y: double - y-coordinate of its center
3. radius: double - radius of the circle
2. You will need to provide virtual getters (accessors) for all attributes.
Note: do not define any setters (mutators)
3. You will need to implement the virtual method getArea() provided by Shape.
The area of a circle is defined as:
3.14 * radius * radius
4. Provide a constructor initializing all attributes, and initializing the
shape's type (provided by the Shape base class) to "circle"
2. Write C++ classes Square. (See attached UML)
Square is a Shape representing a standard square (width = height), with an
x- and y-coordinate for its center and a length.
1. Square should be a subclass of the provided Shape class. It should contain
the following attributes (private visibility)
1. x: double - x-coordinate of its center
2. y: double - y-coordinate of its center
3. length: double - length the square
2. You will need to provide virtual getters (accessors) for all attributes.
Note: do not define any setters (mutators)
3. You will need to implement the virtual method getArea() provided by Shape.
The area of a square is defined as:
length * length
4. Provide a constructor initializing all attributes, and initializing the
shape's type (provided by the Shape base class) to "square"
3. Create a class CircleSquare using multiple inheritance that subclasses both
Circle and Square. (See attached UML)
1. You will need to provide a constructor that initializes all values of its
two parent types (in order):
circle-x, circle-y, radius, square-x, square-y, length
and initializing the type to "circlesquare"
2. This class should override the virtual function getArea(). The area of a
CircleSquare is defined to be the sum of the areas of its two constituent
parts. Note: Please employ code reuse here. Make calls to the functions
defined by its parent classes. Do not duplicate the code to calculate the
size of a Circle or Square.
3. Since CircleSquare is a composite shape, you will need to define
overrides for all functions other than getArea() and getType() to throw
exceptions. An exception should be a literal string with a informative
message. e.g.: "CircleSquare does not have a Center"
4. The final aspect of this program is to write a main function to test
the classes you have created. You will need to make sure you do the following:
1. Create objects for Circle and Square and output (cout) to show that both
classes perform as expected.
2. Create objects for CircleSquare. Output (cout) to show that the function
getArea() performs as expected. Since CircleSquare provides method
overrides that throw exceptions, you will need to use a try-catch to
show that these methods perform as expected (and that you understand
how exceptions work).
Please see the attached UML diagram and provided header file shape.h for the
definition of Shape (Do not change the code for class Shape).
Here is the shape.h:
#include<iostream>
#include<string>
using namespace std;
class Shape {
string type;
public:
Shape(string type) { this->type = type; }
virtual double getArea() = 0;
string getType() { return type; }
};
Explanation / Answer
#include <iostream>
using namespace std;
class Shape {
public:
float width;
float height;
virtual float getarea() {} ; };
class Circle: public Shape {
public:
Circle(float radius);
float getarea() ; };
class Rectangle: public Shape {
public:
Rectangle(float width, float height);
float getarea() ; };
class Square: public Rectangle {
public:
Square(float size); };
Square::Square (float size) : Rectangle(size, size) { }
Rectangle::Rectangle(float width, float height) {
this->width = width; this->height = height; }
Circle::Circle (float radius) {
this->width = this->height = radius * 2.0; }
float Circle::getarea() { return width * width * 3.14159265 / 4.0; }
float Rectangle::getarea() { return width * height ; }
int main (int argc, char **argv) {
Shape * papers[5];
Circle michael(15.0);
Rectangle andrew(34.0,2.0);
Circle ann(2.3);
Square tim(3.33);
Rectangle jonathan(5.61,7.92);
papers[0] = &michael; papers[1] = &andrew; papers[2] = &ann;
papers[3] = &tim; papers[4] = &jonathan;
for (int k=0; k<5; k++) {
cout << "Area is " << papers[k]->getarea() << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.