I am trying to define interface types in C++ using abstract classes and implement them with concrete classes. The problem I am running into is that I cannot both inherit and interface from another interface and inherit the implementation from a base concrete class.
My goal is to be able to define a hierarchy of interfaces which may inherit from less complex base interfaces. I also want to be able to extend the implementation of interfaces by inheriting from concrete classes (like inheriting from TObjectA in the example below).
This is what I have. The error I am getting is "object of abstract class type "TObjectB" is not allowed". I believe I know why, which is because I didn't implement MethodA() in TObjectB. But I really want is to have the implementation provided by a base class (TObjectA) and still have interface hierarchies (IInterfaceB inherits from IInterfaceA). I also don't want to repeat all the inherited interface methods in my derived concreate classes. How can I do this?
class IInterfaceA
{
public:
virtual void MethodA() = 0;
};
class IInterfaceB : IInterfaceA
{
public:
virtual void MethodB() = 0;
};
class TObjectA : public IInterfaceA
{
public:
void MethodA() { cout << "Method A"; }
};
class TObjectB : public TObjectA, public IInterfaceB
{
public:
void MethodB() { cout << "Method B"; }
};
void TestInterfaces()
{
IInterfaceB* b = new TObjectB(); // error: object of abstract class type "TObjectB" is not allowed
b->MethodB();
delete b;
}