从继承的类中实现抽象方法



我正在尝试做一些以前从未真正做过的事情。我基本上有三节课。类A是一个具有纯虚拟方法的抽象类,类B本身就是一个包含与类A中的虚拟方法同名的方法的类。下面的示例是我的代码的一个非常简化的版本。

class A {
virtual int methodA() = 0;
virtual int methodB() = 0;
virtual int methodC() = 0;
};
class B { //B implements A.A() and A.B()
int methodA() { return 0; }; 
int methodB() { return 0; };
};
class C : A, B {
int methodC() { return 0; }; //C implements A.C()
};

我可以编译类C,但当我尝试构造C new C()的类时,我收到一条编译器消息,说"由于以下成员,无法实例化抽象类:int methodA()':is abstract"

有没有一种方法可以通过C类中的多重继承来使用B类实现a类?

编辑:我们的愿望是保持B级混凝土。

您可以使用一些逐个方法的样板文件来完成。

class A {
public:
virtual int methodA() = 0;
virtual int methodB() = 0;
virtual int methodC() = 0;
};
class B { //B implements A.A() and A.B()
public:
int methodA() { return 0; }; 
int methodB() { return 0; };
};
class C : public A, public B {
public:
int methodA() { return B::methodA(); }
int methodB() { return B::methodB(); }
int methodC() { return 0; }; //C implements A.C()
};

演示:http://ideone.com/XDKDW9

相关内容

最新更新