简体   繁体   中英

How can I allocate class memory?

#include <iostream>
#include <vector>
using namespace std;

enum Color { RED, BLUE, YELLOW };

class Shape {
    Color lineColor;
public:
    Color getLineColor() const { return lineColor; }
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual float getLength() const = 0;
};

class Point {
    int x;
    int y;
public : 
    Point(const int _x, const int _y) { x = _x; y = _y; }
};

class ClosedShape : public Shape {
public:
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual float getLength() const = 0;
};

class Polygon : public ClosedShape {

};

class Triangle : public Polygon {
    Point p1;
    Point p2;
    Point p3;
public:
    Triangle(const Point& _p1, const Point& _p2, const Point& _p3)
        :p1(_p1), p2(_p2), p3(_p3) {}
};

class Rectangle : public Polygon {
    Point p1;
    Point p2;
    Point p3;
    Point p4;
public:
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual float getLength() const = 0;
    Rectangle(const Point& _p1, const Point& _p2, const Point& _p3, const Point& _p4)
        :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
};

class ClosedShapeList {
    vector<Polygon*> v;
public:
    void addShape(Rectangle& const r) {
        v.push_back(&r);
    }
    void addShape(Triangle& const t) {
        v.push_back(&t);
    }
};

int main() {
    Point p1(0, 0), p2(0, 10), p3(20, 20), p4(20, 30);

    ClosedShape* const r = new Rectangle(p1, p2, p3, p4);
    ClosedShape* const t = new Triangle(p1, p2, p3);

    ClosedShapeList list{};
    list.addShape(r);
    list.addShape(t);
    delete r;
    delete t;

    list.print();
    cout << list.getTotalArea() << endl;
}

Hi guys. I have a question, I'm making code and in main function, ClosedShape* const r = new Rectangle(p1, p2, p3; p4), ClosedShape* const t = new Triangle(p1, p2; p3), new Rectangle and new Triangle does not work. and the error message said object of abstract class type "Rectangle" is not allowed? Could you help me please?

I'm seeing these errors related to Rectangle when building with clang:

<source>:70:32: error: allocating an object of abstract class type 'Rectangle'
    ClosedShape* const r = new Rectangle(p1, p2, p3, p4);
                               ^
<source>:49:20: note: unimplemented pure virtual method 'clone' in 'Rectangle'
    virtual Shape* clone() const = 0;
                   ^
<source>:50:18: note: unimplemented pure virtual method 'print' in 'Rectangle'
    virtual void print() const = 0;
                 ^
<source>:51:19: note: unimplemented pure virtual method 'getLength' in 'Rectangle'
    virtual float getLength() const = 0;

as the comments have said there are unimplemented virtual methods clone() , print() and getLength() that need to be implemented inside of Rectangle .

Maybe something like this:

class Rectangle : public Polygon {
...
public:
    Shape* clone() const override {
        return new Rectangle(p1, p2, p3, p4);
    }
    void print() const override {
        std::cout << "Rectangle"; // TODO: Actually print stuff
    }
    float getLength() const override {
        return 0.0; // TODO: Actually compute the length
    }
...
};

Then you'll need to implement similar functions for the Triangle class to get things building. It looks like there are a few other bugs here too though:

    ClosedShape* const r = new Rectangle(p1, p2, p3, p4);
    ClosedShape* const t = new Triangle(p1, p2, p3);

    ClosedShapeList list{};
    // 1: addShape takes a Rectangle or Triangle reference but r and t are both ClosedShapes pointers, maybe the interface should accept a Polygon pointer or a ClosedShape pointer? You can convert child classes to parent classes, but can't (in general) convert parent classes to child class.
    list.addShape(r);
    list.addShape(t);

    // 2: Since addShape adds the pointer to the object to the vector, deleting these leaves those pointers dangling. You'll want to move these functions to the end (or maybe in the destructor of the `ClosedShapeList` class).
    delete r;
    delete t;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM