用户如何选择在C+&#x2B中的抽象类成员变量的实现。
我希望我的类“ Shapeholder”的用户能够将“ Shape Shape”成员变量设置为具有相同界面(INT W/H,GetArea()等)的任何类。因此,我相信我通过将函数设置为Virtual(?),并添加了派生的类,以充当形状的各种实现,将基类作为抽象类。
但是,此当前代码在定义了形状形状成员变量的情况下引发了一个错误,称我“无法声明字段'Shapeholder :: Shape'为抽象类型'Shape'”。
在完美的世界中,我希望用户能够交出与他们想要由形状成员变量代表的类相对应的枚举。有办法这样做吗?
是否有比基础更好的方法:我要在这里创建的派生类界面?
我有以下示例程序来说明我的问题:
#include <iostream>
#include <string>
using namespace std;
typedef int shape_type;
enum {SQUARE, TRIANGLE, REGTANGLE};
// Base class
class Shape {
public:
Shape(int h, int w){
height = h;
width = w;
};
virtual int getArea() = 0;
//int getArea(){return -999;};
private:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape {
public:
Rectangle(int h, int w) : Shape(h, w){
height = h;
width = w;
};
int getArea() {
return (width * height);
}
private:
int height;
int width;
};
class Triangle: public Shape {
public:
Triangle(int h, int w) : Shape(h, w){
height = h;
width = w;
};
int getArea() {
return (width * height)/2;
}
private:
int height;
int width;
};
// Main Class
class ShapeHolder {
public:
ShapeHolder(int ver, string s, shape_type st):
dummyInt(ver), dummyString(s), shape(ver, ver){};
void printStuff(){
cout << shape.getArea() << endl;
}
private:
int dummyInt;
string dummyString;
// this type should be dependent on shape_type
Shape shape;
};
int main() {
ShapeHolder sh(10, "test", TRIANGLE);
sh.printStuff();
return 0;
}
I want users of my class "ShapeHolder" to be able to set the "Shape shape" member variable to be any class with the same interface (int w/h, getArea(), etc). Therefore, I believe I made the base class an abstract class by setting a function to virtual (?), and added derived classes to act as the various implementations of shape.
However, this current code throws an error where the Shape shape member variable is defined, saying that I "cannot declare field 'ShapeHolder::shape' to be of abstract type 'Shape'".
In the perfect world, I would love the user to be able to hand in the enum corresponding to the class they want to be represented by the Shape member variable. Is there a way to do this?
Is there a better method all together than the base:derived class interface I am trying to create here?
I have the following example program to illustrate my question:
#include <iostream>
#include <string>
using namespace std;
typedef int shape_type;
enum {SQUARE, TRIANGLE, REGTANGLE};
// Base class
class Shape {
public:
Shape(int h, int w){
height = h;
width = w;
};
virtual int getArea() = 0;
//int getArea(){return -999;};
private:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape {
public:
Rectangle(int h, int w) : Shape(h, w){
height = h;
width = w;
};
int getArea() {
return (width * height);
}
private:
int height;
int width;
};
class Triangle: public Shape {
public:
Triangle(int h, int w) : Shape(h, w){
height = h;
width = w;
};
int getArea() {
return (width * height)/2;
}
private:
int height;
int width;
};
// Main Class
class ShapeHolder {
public:
ShapeHolder(int ver, string s, shape_type st):
dummyInt(ver), dummyString(s), shape(ver, ver){};
void printStuff(){
cout << shape.getArea() << endl;
}
private:
int dummyInt;
string dummyString;
// this type should be dependent on shape_type
Shape shape;
};
int main() {
ShapeHolder sh(10, "test", TRIANGLE);
sh.printStuff();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的类
Shape
是一个抽象类,缺少定义的纯虚拟
函数。通过将
Shape Shape
转换为Shapeholder
类,将其修复到指针中,它允许使用任何派生的shape
类。另外,标记
width
&amp;height
base类内部的变量受保护允许派生类访问它们。Your class
Shape
is an abstract class and lacks definition's for it'spure virtual
function.Fix this by converting
Shape shape
inside yourShapeHolder
class to a pointer instead, whichs allows any derivedShape
class to be used.Also, marking the
width
&height
variable's inside the base classprotected
allows derived classes to access them.在课堂中声明成员函数
,您无法实例化该类的对象。pure virtual
因此,在您的
Shapeholder
类中,将Shape变量声明为Shape Shape Shape
将导致编译错误。您不能直接创建抽象类的变量,但是可以保留指针或对抽象类的引用。
抽象类就像抽象类和派生类之间的接口/合同。从抽象类得出的任何类都必须实现此接口/合同。
如下更改您的基类:
在您的
中,三角形
和矩形
类无需具有width
和height> height
accope 变量,他们将可以访问基类width
和height
。因此,在
Main
函数中:输出:
In a class once you declare a member function
pure virtual
, you can't instantiation an object of that class.So in your
ShapeHolder
class, declaring the shape variable asShape shape
will cause a compilation error.You can't directly create a variable of abstract class but you can keep a pointer or reference to abstract class.
Abstract class is like an interface/contract between abstract class and derived class. Any class which is derived from abstract class must implement this interface/contract.
Change your base class as follows:
In your
Triangle
andRectangle
classes no need to havewidth
andheight
variables, they will have access to base classwidth
andheight
. SoIn the
main
function:Output: