# Untitled

unknown
plain_text
14 days ago
2.0 kB
2
Indexable
Never
```#ifndef POLYGON_H
#define POLYGON_H

#include <vector>

class Point {
public:
int x, y;
Point(int x, int y) : x(x), y(y) {}
};

class Polygon {
private:
std::vector<Point *> pts;
public:
Polygon(std::vector<Point *> pts);
Polygon(Point p1, Point p2, Point p3);
Polygon(const Polygon &other);
~Polygon();
Point operator[](int index);
Polygon operator+(const Point &pt);
};

#endif

#include "Polygon.h"

Polygon::Polygon(std::vector<Point *> pts) : pts(pts) {}

Polygon::Polygon(Point p1, Point p2, Point p3) {
pts.push_back(new Point(p1));
pts.push_back(new Point(p2));
pts.push_back(new Point(p3));
}

Polygon::Polygon(const Polygon &other) {
for (Point *pt : other.pts) {
pts.push_back(new Point(pt->x, pt->y));
}
}

Polygon::~Polygon() {
for (Point *pt : pts) {
delete pt;
}
}

pts.push_back(pt);
}

Point Polygon::operator[](int index) {
return *(pts[index]);
}

Polygon Polygon::operator+(const Point &pt) {
Polygon newPolygon(*this);
return newPolygon;
}

#include "Polygon.h"
#include <iostream>

int main() {
Point p1(0, 0);
Point p2(1, 1);
Point p3(2, 2);

// Creating a polygon using the constructor with three points
Polygon polygon1(p1, p2, p3);

// Creating a polygon using the vector of points constructor
std::vector<Point *> points = { &p1, &p2, &p3 };
Polygon polygon2(points);

// Copying polygon1 to polygon3
Polygon polygon3(polygon1);

// Adding a new point to polygon1
Point p4(3, 3);