关于在对象指针数组中使用sort函数对成员排序(重载运算符小于号)

问题遇到的现象和发生背景

img


关于第五条我尝试了很多方法去使用sort函数,不是无法比较出来就是出现断点,没有使用sort函数前,打印相关的成员都是几十的整数,使用以后变成了-的八十几万,在主函数注释掉的代码似乎揭示上述原因是子类对象赋给父类对象的过程转换上出了问题

用代码块功能插入代码,请勿粘贴截图
#include<iostream>
#include<algorithm>
#include<fstream>

using namespace std;

class Plane {

protected:

    int x;
    int y;

public:
    int s;
    Plane(int x1, int y1,int s1 = 0) :x(x1), y(y1),s(s1)

    {
        printf("Plane构造函数被调用\n ");
    }

    Plane() {
        x = 0;
        y = 0;
        s = 0;
        printf("Plane构造函数被调用\n ");
    }

    Plane(const Plane& p)

    {
        x = p.x;
        y = p.y;
        printf("Plane拷贝构造函数被调用\n ");
    }

    ~Plane()
    {
        printf("Plane析构函数被调用\n ");
    }
    

    void Setx(int x1)

    {

        x = x1;

    }

    void Sety(int y1)

    {

        y = y1;

    }
    int Getx()

    {

        return x;

    }

    int Gety()

    {

        return y;

    }
    void Sets(int s1) {
        s = s1;
    }
    int  Gets()
    {
        return s;
    }


    virtual int area() {
        return 0;
    }
};
bool operator <(const Plane& p1,const Plane& p2) {
    if (p1.s < p2.s)
        return true;
    else
        return false;
}




class Point {

protected:
    int x, y;
public:

    Point(int x1, int y1) :x(x1), y(y1)

    {
        printf("Point构造函数被调用\n");
    }

    Point() {
        x = 0;
        y = 0;
        printf("Point构造函数被调用\n");
    }

    Point(const Point& p)

    {
        x = p.x;
        y = p.y;
        printf("Point拷贝构造函数被调用\n ");
    }

    ~Point()

    {
        printf("Point析构函数被调用\n ");
    }

    void Setx(int x1)

    {
        x = x1;
    }

    void Sety(int y1)
    {
        y = y1;
    }

    int Getx()
    {
        return x;
    }

    int Gety()
    {
        return y;
    }

};





class Circle :public Plane {

protected:
    Point* x;
    int r;
public:
    Circle(int x1, int y1, int r1)
    {
        x = new Point(x1, y1);
        r = r1;
        printf("Circle构造函数被调用\n ");
    }

    Circle() {
        x = new Point(0, 0);
        r = 0;
        printf("Circle构造函数被调用\n ");
    }

    Circle(const Circle& p)

    {
        x = p.x;
        r = p.r;
        printf("Circle拷贝构造函数被调用\n ");
    }

    ~Circle()

    {
        if (x)
            delete x;
        printf("Circle析构函数被调用\n ");
    }

    void Setpoint(int x1,int y1)

    {
        x->Setx(x1);
        x->Sety(y1);
    }



    Point& Getpoint()

    {
        return *x;
    }
    void Setr(int r1)

    {
        r = r1;
    }

    int Getr()

    {
        return r;
    }

    virtual int area()

    {   
        s = 3.14 * r * r;
        return s;
    }
};





class Rectangle :public Plane {

protected:

    Point* left_up;
    Point* right_down;

public:

    Rectangle(int x1, int y1, int x2, int y2)

    {
        left_up = new Point(x1, y1);
        right_down = new Point(x2, y2);
        printf("Rectangle构造函数被调用\n ");
    }

    Rectangle() {
        left_up = new Point(0, 0);
        right_down = new Point(0, 0);
        printf("Rectangle构造函数被调用\n ");
    }

    Rectangle(const Rectangle& p)

    {
        left_up = p.left_up;
        right_down = p.right_down;
        printf("Rectangle拷贝构造函数被调用\n ");
    }

    ~Rectangle()

    {
        if (left_up)
            delete left_up;
        if (right_down)
            delete right_down;
        printf("Rectangle析构函数被调用\n ");
    }

    virtual int area()

    {
        s = abs(left_up->Getx() - right_down->Getx()) * abs(left_up->Gety() - right_down->Gety());
       // Sets(abs(left_up->Getx() - right_down->Getx()) * abs(left_up->Gety() - right_down->Gety()));
        return s;//
    }

    void Setleft_up(int x1, int y1)

    {
        left_up->Setx(x1);
        left_up->Setx(y1);
    }

    void Setright_down(int x2, int y2)

    {
        right_down->Sety(x2);
        right_down->Sety(y2);
    }
    Point& Getleft_up() {
        return *left_up;
    }
    Point& Getright_down() {
        return *right_down;
    }


  /* Rectangle operator + (const Rectangle& p)

    {
        Rectangle temp;
        temp.Setx(left_up->Getx() + abs(left_up->Getx() - right_down->Getx()) + abs(p.left_up->Getx() - p.right_down->Getx()), right_down->Getx());

        temp.Sety(left_up->Gety() + abs(left_up->Gety() - right_down->Gety()) + abs(p.left_up->Gety() - p.right_down->Gety()), right_down->Gety());

        return temp;

    }
*/


  //  friend Rectangle operator*(const Rectangle& p1, const Rectangle& p2);





};





/*Rectangle operator * (const Rectangle& p1, const  Rectangle& p2)

{

    Rectangle temp;

    temp.Setx(p1.left_up->Getx() + (abs(p1.left_up->Getx() - p1.right_down->Getx()) * abs(p1.left_up->Gety() - p1.right_down->Gety())), p1.right_down->Getx());

    temp.Sety(p1.left_up->Gety() + (abs(p2.left_up->Getx() - p2.right_down->Getx()) * abs(p2.left_up->Gety() - p2.right_down->Gety())), p1.right_down->Gety());

    return temp;



}*/



int main() {
    /*   Plane* p1 = new Circle(1, 1, 4);
       Plane* p2 = new Circle(5, 2, 3);
      //Plane arrayOfShapes[4] = { Plane(Circle(1, 1, 4)) ,Plane( Circle(5, 2, 3) ),Plane(Rectangle(3, 4, 4, 3) ),Plane(Rectangle(5, 10, 8, 8)) };
      Plane arrayOfShapes[2] = { *p1,*p2};
      for (int i = 0; i < 2; i++) {
          arrayOfShapes[i].area();
          cout << arrayOfShapes[i].s << endl;
      }
      return 0;

      */

    Circle circle1(1, 1, 4);

    Circle circle2(2, 2, 1);

    Rectangle rectangle1(3, 3, 4, 4);

    Rectangle rectangle2(5, 5, 6, 6);
    Plane* arrayOfShapes[6];

    arrayOfShapes[0] = &circle1;

    arrayOfShapes[1] = &circle2;

    arrayOfShapes[2] = &rectangle1;

    arrayOfShapes[3] = &rectangle2;
    for (int i = 0; i < 4; ++i)

    {
        arrayOfShapes[i]->area();
       
        cout << arrayOfShapes[i]->Gets() << endl;
      
        cout << endl;

    }
 sort(*arrayOfShapes[0],*arrayOfShapes[3]);
    for (int i = 0; i < 4; ++i)

    {
        cout << arrayOfShapes[i]->Gets() << endl;
    }
}