很难让标题清楚地说明主题,但我会尝试解释上下文(下面有一些代码)。注意:我看到类似的问题得到了回答,但他们只处理了 1 个 child 类(class)的案例。所以他们对我的情况并没有真正的帮助,因为我有 2 个子类(class)。
上下文: 我有一个父类 Shape,它有 2 个子类:Circle 和 Square。 我将有一个 Shape 对象的 vector ,但这些 Shape 对象实际上只是 Circle 对象或 Square 对象。我需要 Circle 和 Square 类具有相同的父类,以便我可以将它们存储在同一个 vector 中。
诀窍是我需要使用 vector 中的 Shape 对象来调用在 Circle 类或 Square 类中实现的方法,因此,我需要在父类中有这些方法的“虚拟”版本形状。
这是我的类的代码的简化部分:
形状.h :
class Shape{
public:
std::string getColor();
virtual int getRadius() = 0; //Method implemented in Circle
virtual int getHeight() = 0; //Method implemented in Square
virtual int getWidth() = 0; //Method implemented in Square
protected:
std::string color;
};
class Circle : public Shape{
public:
int getRadius();
private:
int radius;
};
class Square : public Shape{
public:
int getHeight();
int getWidth();
private:
int height;
int width;
};
在 Shape.cpp 中我有这样的东西:
std::string Shape::getColor(){
return color;
}
int Circle::getRadius(){
return radius;
}
int Square::getHeight(){
return height;
}
int Square::getWidth(){
return width;
}
当我想创建 Circle 和 Square 对象时,main.cpp 中出现了错误:
Circle *c = new Circle(...);//Error: cannot instantiate abstract class
//pure virtual function "Shape::getHeight" has no overrider
//pure virtual function "Shape::getWidth" has no overrider
Square *s = new Square(...);//Error: cannot instantiate abstract class
//pure virtual function "Shape::getRadius" has no overrider
看来我需要在 Square 类中声明“getRadius”,在 Circle 类中声明“getHeight”和“getWidth”...
我尝试用 virtual 添加它们,但这会生成 Circle 和 Square 抽象类,所以我不能用它们创建任何对象。
有没有办法让它工作?
这是我在 stackoverflow 上发布的第一个问题。我希望一切都清楚。感谢您的帮助!
最佳答案
您的虚拟方法并不是真正适合虚拟方法的候选对象,因为它们对一个类具有特定功能但对另一个类没有用处。
虚方法的一个很好的例子是由每个类实现但具有不同功能或结果的东西,比如 virtual int area()
或 virtual bool intersects( Shape * otherShape )
等等。
无论如何,这就是您编译代码的方式(还有一些额外的):
形状:
class Shape{
public:
std::string getColor();
Shape() {}
virtual ~Shape() {}
virtual int getRadius() { return 0; } // no pure virtual
virtual int getHeight() { return 0; } // no pure virtual
virtual int getWidth() { return 0; } // no pure virtual
protected:
std::string color;
};
class Circle : public Shape {
public:
Circle( int r )
: Shape()
, radius( r )
{}
Circle() : Circle( 0 ) {}
~Circle() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
int getRadius() override { return radius; };
private:
int radius;
};
正方形:
class Square : public Shape {
public:
Square( int h, int w )
: Shape()
, height( h )
, width( w )
{}
Square() : Square( 0, 0 ) {}
~Square() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
int getHeight() override { return height; }
int getWidth() override { return width; }
private:
int height;
int width;
};
测试:
int main() {
using shapes = std::vector< Shape * >;
shapes s;
s.push_back( new Circle( 10 ) );
s.push_back( new Square() );
s.push_back( new Square( 1, 3 ) );
s.push_back( new Circle() );
for ( Shape * sh : s ) {
std::cout
<< " r " << sh->getRadius()
<< " h " << sh->getHeight()
<< " w " << sh->getWidth()
<< std::endl;
}
for ( Shape * sh : s ) { delete sh; } s.clear();
}
输出:
r 10 h 0 w 0
r 0 h 0 w 0
r 0 h 1 w 3
r 0 h 0 w 0
virtual Circle::~Circle()
virtual Square::~Square()
virtual Square::~Square()
virtual Circle::~Circle()
下面是一个更好地使用虚拟方法的区域示例:
#include <iostream>
#include <vector>
struct Shape {
Shape() {}
virtual ~Shape() {}
virtual double area() = 0;
};
扩展不同区域的实现:
struct Circle : public Shape {
Circle( int r )
: Shape()
, radius( r )
{}
Circle() : Circle( 0 ) {}
~Circle() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
virtual double area() override { return radius * radius * 3.14; }
int radius;
};
struct Square : public Shape {
Square( int h, int w )
: Shape()
, height( h )
, width( w )
{}
Square() : Square( 0, 0 ) {}
~Square() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
virtual double area() override { return height * width; }
int height;
int width;
};
测试
int main() {
using shapes = std::vector< Shape * >;
shapes s;
s.push_back( new Circle( 1 ) );
s.push_back( new Square( 1, 1 ) );
s.push_back( new Square( 2, 3 ) );
s.push_back( new Circle( 2 ) );
for ( Shape * sh : s ) {
std::cout << sh->area() << std::endl;
}
for ( Shape * sh : s ) { delete sh; } s.clear();
}
输出:
3.14
1
6
12.56
virtual Circle::~Circle()
virtual Square::~Square()
virtual Square::~Square()
virtual Circle::~Circle()
关于具有在 2 个不同子类中实现的虚拟方法的 C++ 父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031385/