Файл figure.h #pragma once #include <iostream> #include <vector> #include <map> #include…
- User
Файл figure.h #pragma once #include #include #include #include "singleton.h" using std::vector;
class FigureFactory :public Singleton<FigureFactory> { public: typedef Figure* (*CreateFigureCallback)(); private: typedef std::map<int, CreateFigureCallback> CallbackMap; CallbackMap callbacks; public: bool RegisterFigure(int FigureID, CreateFigureCallback CreateFN) { return callbacks.insert(CallbackMap::value_type(FigureID, CreateFN)).second; } bool UnregisterFigure(int FigureID); Figure* CreateFigure(int FigureID) { CallbackMap::const_iterator i = callbacks.find(FigureID); if (i == callbacks.end()) { throw std::runtime_error("Unknown typeID"); } return (i->second)(); } friend class Singleton<FigureFactory>; }; class Figure { protected: int color; virtual void Print(std::ostream& stream) const = 0; friend std::ostream& operator<<(std::ostream& stream, const Figure& f) { f.Print(stream); return stream; } public: Figure(int color) : color(color) {}; virtual double place() const = 0; virtual double perimetr() const = 0; struct Point { int x, y; Point(int x, int y) : x(x), y(y) {}; }; }; class Circle : public Figure { Point center; int rad; public: Circle(Point center, int rad, int color); virtual double place() const; virtual double perimetr() const; Point getCenter(); double getRad(); int getColor() const { return color; } protected: void Print(std::ostream& stream) const; }; class Triangle : public Figure { Point p1, p2, p3; public: Triangle(Point p1, Point p2, Point p3, int color); virtual double place() const; virtual double perimetr() const; Point getP1() const; Point getP2() const; Point getP3() const; int getColor() const { return color; } protected: void Print(std::ostream& stream) const; }; class Polygon : public Figure { vector<Point> points; int numPoints; public: Polygon(vector<Point> points, int numPoints, int color); virtual double place() const; virtual double perimetr() const; vector<Point> getPoints() const; int getSize() const; protected: void Print(std::ostream& stream) const; }; - Assistant
The figure.h file defines the FigureFactory class and the Figure, Circle, Triangle, and Polygon classes.
The FigureFactory class is a singleton class that registers and creates instances of Figure objects. It has a map of figure IDs and create function callbacks. It provides methods to register figure IDs and create figures based on their IDs.
The Figure class is an abstract base class for all types of figures. It has a color member variable and pure virtual methods for calculating the area and perimeter of the figure. It also has a nested Point struct to represent coordinates.
The Circle class is a subclass of Figure that represents a circle. It has additional member variables for the center and radius of the circle. It implements the virtual methods for calculating the area and perimeter of the circle.
The Triangle class is a subclass of Figure that represents a triangle. It has additional member variables for the three vertices of the triangle. It implements the virtual methods for calculating the area and perimeter of the triangle.
- User
- Assistant
The figure.cpp file implements the member functions of the Circle, Triangle, and Polygon classes, as well as the UnregisterFigure function of the FigureFactory class.
The Circle class has implementations for the place, perimetr, and Print functions. The place function calculates the area of the circle using the formula pi * r^2, where r is the radius. The perimetr function calculates the perimeter of the circle using the formula 2 * pi * r. The Print function prints the color and center coordinates of the circle.
The Triangle class has implementations for the place, perimetr, and Print functions. The place function calculates the area of the triangle using the shoelace formula. The perimetr function calculates the perimeter of the triangle using the distance formula. The Print function prints the color and coordinates of the three vertices of the triangle.
The Polygon class has implementations for the place, perimetr, and Print functions. The place function calculates the area of the polygon using the shoelace formula. The perimetr function calculates the perimeter of the polygon by summing the distances between consecutive points. The Print function prints the number of points in the polygon.
- User
У меня ошибки компиляции. Помоги исправить код
- Assistant
Конечно! Я могу помочь вам исправить код. Но для этого мне нужна информация о возникающих ошибках компиляции, а также сообщения об ошибках. Пожалуйста, предоставьте мне эту информацию, и я постараюсь помочь вам исправить код.