虚拟基类的派生类列表

A list of derived classes of a virtual base class

本文关键字:列表 派生 基类 虚拟      更新时间:2023-10-16

我尝试使用其中2种方法创建一个基本虚拟类"形状"。下面我创建了3个从基类继承的子类。在主要函数中,我创建了3个实例,每个子类,我想拥有从我的基类创建的所有子类的列表,该列表应通过基类中定义的任何一种虚拟方法来排序。

我试图根据互联网的一些示例来实现列表库,但我不太了解它以使其正常工作。

#include <iostream>
#include <cmath>
#include <conio.h>
#include <list>
using namespace std;
class Shape
{
protected:
int value_;
public:
   /*static list<Shape*> instances_;
   Shape(int val);
   static void showList();*/
   virtual void surface()=0;
   virtual void circuit()=0;
};
/*Shape::Shape(int val) {
   instances_.push_back(this);
   value_ = val;}
list<Shape*> Shape::instances_;
void Shape::showList() {
   for (list<Shape*>::iterator p = instances_.begin();
        p != instances_.end(); ++p)
      cout << (*p)->value_ << endl;}*/
//////////////////////////////////
class Circle :public Shape
{
   float r;
public:
   Circle(float x)
   {
      r=x;
   }
   virtual void surface()
   {
      cout<<"Circle surface: "<<3.14*r*r<<endl;
   }
   virtual void circuit(){
      cout<<"Circle circuit: "<<3.14*2*r<<endl;
   }
};
////////////////////////////////////////
class Square:public Shape
{
   float a;
public:
   Square:public (float x)
   {
      a=x;
   }
   virtual void surface()
   {
      cout<<"Square surface: "<<a*a<<endl;
   }
    virtual void circuit(){
      cout<<"Square circuit : "<<4*a<<endl;
   }
};
int main()
{
    Circle k(8);
    Square kw(2);

   return 0;
};

此代码中有几件事可以改进。但是,您必须了解继承,多态性和在C 中实施多态性背后的粗略细节的概念。

例如:默认的基类破坏者不是自动虚拟的。当您打算实现多态性结构时,您需要指定虚拟基类驱动器。派生的类自动设置为虚拟iff(仅以及仅当(基类带有虚拟驱动器。

要排序您首先需要创建Shape*列表的形状列表。然后,您需要编写一个比较功能,该功能将采用2种形状,并使用其成员之一比较它们。在您的示例中,没有什么可用的。两个成员函数均为void,成员变量均为私有。

请参阅下面的代码,希望它将有意义。

#include <iostream>
#include <cmath>
#include <list>
struct Shape
{
    Shape() = default;
    virtual float surface() const = 0;
    virtual void circuit() = 0;
    virtual ~Shape() noexcept = default;
};
class Circle final : public Shape
{
    private:
        float r;
    public:
        Circle(float r_) : r(r_)
        {;}
        float surface() const override
        {
            return M_PI*pow(r,2);
        }
        void circuit() override
        {
            std::cout<<"Circle circuit: " << 2.f*M_PI*r <<std::endl;
        }
        ~Circle() noexcept = default;
};
class Square final : public Shape
{
    private:
        float a;
    public:
        Square(float a_) : a(a_)
        {;}
        float surface() const override
        {
            return pow(a,2);
        }
        void circuit() override
        {
            std::cout<<"Square circuit: " << 4.f*a <<std::endl;
        }
        ~Square() noexcept = default;
};

bool compare_shape(const Shape *const s1, const Shape *const s2)
{
    return (s1->surface() < s2->surface());
}
int main()
{
    // no polymorphism
    Circle c(8);
    std::cout<< "Circle surface: " << c.surface() <<std::endl;
    c.circuit();
    Square s(2);
    std::cout<< "Square surface: " << s.surface() <<std::endl;
    s.circuit();
    std::cout<< "____________________" <<std::endl<<std::endl;
    // polymorphism
    Shape *c_ptr = new Circle(8);
    std::cout<< "Circle surface: " << c_ptr->surface() <<std::endl;
    c_ptr->circuit();
    delete c_ptr;
    Shape *s_ptr = new Square(2);
    std::cout<< "Square surface: " << s_ptr->surface() <<std::endl;
    s_ptr->circuit();
    delete s_ptr;
    std::cout<< "____________________" <<std::endl<<std::endl;
    // list of Shapes
    std::list<Shape*> shapes;
    shapes.push_back( new Circle(8) );
    shapes.push_back( new Square(2) );
    for (auto &&i : shapes)
    {
        std::cout<< "Shapes' surface: " << i->surface() <<std::endl;
        i->circuit();
    }
    std::cout<< "n-- sorting the list based on the shapes' surface.n" <<std::endl;
    shapes.sort(compare_shape);
    for (auto &&i : shapes)
    {
        std::cout<< "Shapes' surface: " << i->surface() <<std::endl;
        i->circuit();
    }
};

在线代码示例:https://rextester.com/ebkih52610

如果要以多态性的方式进行排序,请搜索对``bool Operator&lt;(('方法的搜索文档。要比较类型,您可以使用'typeID'operator。