简体   繁体   English

制作基类指针的向量并将派生类对象传递给它(多态)

[英]Making a vector of base class pointers and pass Derived class objects to it (Polymorphism)

I'm trying to implement a menu for my Shape program.我正在尝试为我的 Shape 程序实现一个菜单。 I've implemented all of the shapes classes.我已经实现了所有的形状类。 Two are deriving straight from the abstract class "Shape", and two others are deriving from a class called "Polygon" which derives from "Shape" as shown bellow:两个直接派生自抽象类“Shape”,另外两个派生自一个名为“Polygon”的类,该类派生自“Shape”,如下所示:

Shape -> Polygon -> Rectangle, Triangle
  `-> Circle, Arrow

In my menu class, I want to create some sort of an array that can contain the pointers to the objects and with the type of the base class "Shape".在我的菜单类中,我想创建某种类型的数组,该数组可以包含指向对象的指针和基类“Shape”的类型。 But I'm not sure how to do it properly and in a way which will work for all of my shapes, because 2 of my classes aren't deriving from "Shape" directly.但我不确定如何正确地以一种适用于我所有形状的方式来做,因为我的 2 个课程不是直接从“形状”派生的。

This is my menu class:这是我的菜单类:

class Menu
{
protected:
    //array of derived objects 
public:

    Menu();
    ~Menu();

    // more functions..
    void addShape(Shape& shape);
    void deleteAllShapes();
    void deleteShape(Shape& shape);
    void printDetails(Shape& shape);
private:
    Canvas _canvas; //Ignore, I use this program to eventually draw this objects to a cool GUI
};

And in the function "addShape(Shape& shape);", Which I want to use to add each given shape to my array.在函数“addShape(Shape& shape);”中,我想用它来将每个给定的形状添加到我的数组中。 How can I implement the addition of new objects to it?如何实现向其添加新对象? And also, how can I check if the given object is deriving from "Polygon" or not?而且,我如何检查给定的对象是否来自“多边形”? Because if so then I need to call the member functions differently as far as I understand.因为如果是这样,那么据我所知,我需要以不同的方式调用成员函数。

I see that you have an array in Menu, let's say:我看到你在菜单中有一个数组,让我们说:

Shape* myshapes[10];

The shapes can be Rectangles, Triangles, Circles etc. What you want is to be able to use the Menu's printDetails() method like this:形状可以是矩形、三角形、圆形等。您想要的是能够像这样使用菜单的 printDetails() 方法:

    void printDetails()
    {
        for(int i = 0; i < size; i++)
        {
            cout << "Index " << i << " has " << myshapes[i]->getShapeName() << endl;
        }
    }

The getShapeName() will return a string, eg "Rectangle" if it is Rectangle. getShapeName() 将返回一个字符串,例如“矩形”,如果它是矩形。 You will be able to do this with the help of pure virtual function.您将能够在纯虚函数的帮助下做到这一点。 The pure virtual function must be in the abstract class Shape, which has:纯虚函数必须在抽象类 Shape 中,它具有:

virtual string getShapeName() = 0; //pure virtual

It means that we are expecting a definition for this function in the derived class.这意味着我们期望在派生类中定义此函数。 This way you will be able to use getShapeName() method using the Shape pointers in the shapes array, which will tell you whether the shape is Rectangle, Triangle, or Circle etc.通过这种方式,您将能够使用形状数组中的形状指针使用 getShapeName() 方法,该方法将告诉您形状是矩形、三角形还是圆形等。

class Shape
{
    public:
    virtual string getShapeName() = 0;
};

class Circle : public Shape
{
    private:
    int radius;

    public:
    Circle(int r) { radius = r; cout << "Circle created!\n"; }
    string getShapeName() { return "Circle"; }
};

class Arrow : public Shape
{
    private:
    int length;

    public:
    Arrow(int l) { length = l; cout << "Arrow created!\n"; }
    string getShapeName() { return "Arrow"; }
};

class Polygon : public Shape
{
    public:
    virtual string getShapeName() = 0;
};

class Triangle : public Polygon
{
    private:
    int x, y, z;

    public:
    Triangle(int a, int b, int c) { x = a; y = b; z = c; cout << "Triangle created!\n"; }
    string getShapeName() { return "Triangle"; }
};

class Rectangle : public Polygon
{
    private:
    int length;
    int width;

    public:
    Rectangle(int l, int w){ length = l; width = w; cout << "Rectangle created!\n"; }
    string getShapeName() { return "Rectangle"; }
};

To implement the addShape() method you can do this:要实现 addShape() 方法,您可以这样做:

void addShape(Shape &shape)
{
    myshapes[count] = &shape;
    count++;
}

Also, keep in mind to pass the Shape by reference or by using pointer, in the addShape() method.另外,请记住在 addShape() 方法中通过引用或使用指针传递 Shape。 I hope this helps... Best of luck :-)我希望这会有所帮助...祝你好运:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在指向基类对象的指针向量中引用派生对象? - How to reference derived objects in a vector of pointers to base class objects? 指向包含基类和派生类 class 对象的指针的向量 - 访问派生类特定变量 - Vector of pointers to base class containing base and derived class objects - accessing derived-class specific variables 使指向基类指针std :: vector的指针指向派生类指针的std :: vector - Making a pointer to std::vector of base class pointers point to std::vector of derived class pointers 指向矢量的基类在派生类中 - Vector of pointers to base class within a derived class 使用基类指针派生类对象 - Using Base Class Pointers to Derived Class Objects 多态基类指针 - Polymorphism base class pointers 将具有映射的抽象基类派生的对象存储在基类指针的向量数组中 - Storing a objects derived from an abstract base class with maps in a vector array of base class pointers 通过指向基类对象的指针来操作派生类对象的指针 - Manipulating with pointers to derived class objects through pointers to base class objects 我想要一个派生类指针的向量作为基类指针 - I want a vector of derived class pointers as base class pointers 将派生类指针的矢量传递给线程 - Pass vector of derived class pointers to thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM