1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#pragma once
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
using namespace std;
bool CompareArea(const Point* lhs, const Point* rhs)
{
return lhs->area() < rhs->area();
}
class Point {
public:
Point(double xcoord = 0.0, double ycoord = 0.0);
virtual ~Point() = default;
virtual void input(const char* prompt);
virtual void output() const;
virtual double area() const;
void move(double deltax, double deltay);
private:
double x;
double y;
};
class Circle : public Point {
public:
Circle(double xcoord = 0.0, double ycoord = 0.0, float r0 = 0.0) : Point(xcoord, ycoord) { radius = r0; }
virtual ~Circle() = default;
void input(const char* prompt);
void output() const;
double area() const;
private:
float radius;
};
class Square : public Point {
public:
Square(double xcoord = 0.0, double ycoord = 0.0, float w = 0.0, float h = 0.0) : Point(xcoord, ycoord) { width = w, height = h; }
virtual ~Square() = default;
void input(const char* prompt);
void output() const;
double area() const;
private:
float width;
float height;
};
int main()
{
vector<Point*> shapes;
Point pt;
Circle cir;
Square sq;
shapes.push_back(new Point(1, 1));
shapes.push_back(new Circle(2, 2, 2));
shapes.push_back(new Square(5, 5, 2, 2));
sq.input("Enter square coordinates and height, width");
shapes.push_back(&sq);
cir.input("Enter circle coordinates and radius");
shapes.push_back(&cir);
pt.input("Enter point coordinates");
shapes.push_back(&pt);
for (auto p : shapes)
p->output();
sort(shapes.begin(), shapes.end(), CompareArea);
for (auto p : shapes)
p->output();
return 0;
}
|