-1

I have one base class and I have 4 class derived.of course I write 2 class for example. I created vector and fill it by objects of derived classes, then I want sort my vector base of area function. I want use operator overloading.

I define operator overloading but it not Completely! please help me! Thanks....

class Shape
    {
    public:
       Shape() {}
       virtual void draw() = 0;
       virtual int area() = 0;
       virtual void perimeter() = 0;
       bool operator >(Shape * shape_one , Shape* shape_two )
       {
           return shape_one->area() > shape_two->area();
       }

    protected :
        double height;
        double width;

    };

class Squar : public Shape
{
public:
    Squar(int _width) {
      width = _width;
    }
    void draw(){}
    int area(){
        return width * width;
    }

    void perimeter(){}
    bool operator >(Squar * squar_one , Squar* squar_two )
    {
        return squar_one->area() > squar_two->area();
    }

};

class Triangle : public Shape
{
public:
    Triangle(int _width , int _height) {
        width  = _width;
        height = _height;
    }
    void draw(){}
    int area(){

        return (width * height) / 2;
    }
    void perimeter(){}
    bool operator >(Triangle * triangle_one , Triangle* triangle_two )
    {
        return triangle_one->area() > triangle_two->area();
    }
};

int main()
{    
    Shape *rect         = new Rectangular( 1 , 9);
    Shape *squar        = new Squar(5);

    QVector <Shape *> list;
    list.push_back(rect);
    list.push_back(squar);
retuurn 0;
}

1 Answers1

0

I understand answer the question.

add following code in base class :

bool operator <( Shape *item )
   {
       return this->area() < item->area();
   }

and add following code in main :

std ::sort(list.begin() , list.end() ,[](Shape* ptr_l , Shape* ptr_r) { return *ptr_l < ptr_r;} );

This code is correct! :)