我试图用两个方法创建一个基本的虚拟类“ Shape”。下面,我创建了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;
};
答案 0 :(得分: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();
}
};
答案 1 :(得分:-1)
如果要以多态方式进行排序,请搜索有关重载`bool operator <()'方法的文档。要比较类型,可以使用'typeid'运算符。