C++ is it possible to make a class extend one class and be a realization of an interface at the same time?

12,003

Solution 1

A bit useful example: :-)

class CImplementation
{
public:
void doFoo();
};

void CImplementation::doFoo()
{
//implementation
}

class IInterface
{
public:
virtual void foo()=0;
};

class DerivedFromImplementationAndInterface : public CImplementation, public IInterface
{
virtual void foo();
};

void DerivedFromImplementationAndInterface::foo()
{
doFoo();
}


//possible usage:
void method(IInterface& aInterface)
{
aInterface.foo();
}

void test()
{
IInterface* d = new DerivedFromImplementationAndInterface;
method(*d);
}

Solution 2

C++ doesn't explicitly have interfaces, the equivalent of an interface in Java is usually implemented with a class having only pure virtual functions (plus constructors, destructor, copy assignment):

#include <iostream>

// interface
class Fooable {
  public:
    virtual int foo() = 0;
    virtual ~Fooable() {}
};

// base class
class Base {
  public:
    void non_virtual_function() { std::cout << "Base::non_virtual_function\n"; }
    virtual void virtual_function() { std::cout << "Base::virtual_function\n"; }
};

// derived class, inherits from both Base "class" and Fooable "interface"
class Derived: public Base, public Fooable {
  public:
    virtual int foo() {
      // call base class function
      Base::non_virtual_function();
      // virtual call to function defined in base class, overridden here
      virtual_function();
    }
    virtual void virtual_function() {
        // call base class implementation of virtual function directly (rare)
        Base::virtual_function();
        std::cout << "Derived::virtual_function\n";
    }
    void non_virtual_function() {
        // not called
        std::cout << "Derived::non_virtual_function\n";
    }
};

int main() {
    Derived d;
    d.foo();
}

Solution 3

In C++, you can extend multiple classes, it's called multiple inheritance. Most probably this is what you're looking for. Please read a good book about multiple inheritance and C++ (a quick introduction: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm), because there are many pitfalls and details to pay attention to.

Example for multiple inheritance:

class A { ... };
class B { ... };
class C: public A, public B {};  // C inherits from A and B.

Solution 4

Not sure what you're asking:

class A
{
  public:
    void method();
};

class B
{
  public:
    void method();
};

class C : public A, public B
{
  public:
    void callingMethod();
};

void C::callingMethod()
{
  // Here you can just call A::method() or B::method() directly.
  A::method();
  B::method();
}

Note that multiple inheritance can lead to really hard-to-solve problems and I would recommend to only use it when necessary.

Solution 5

The question as stated,

C++ is it possible to make a class extend one class and implement another?

does not make much sense. The answer to that is just "yes". You can derive from any number of classes: C++ fully support multiple inheritance.

So, given that the question as stated isn't really meaningful, it's at least possible that you meant to ask

C++ is it possible to make a class extend one class and thereby implement another?

The answer to this question is also yes, but it's not trivial. It involves virtual inheritance. Which is quite tricky.

Here's an example:

#include <iostream>

void say( char const s[] ) { std::cout << s << std::endl; }

class TalkerInterface
{
public:
    virtual void saySomething() const = 0;
};

class TalkerImpl
    : public virtual TalkerInterface
{
public:
    void saySomething() const
    {
        say( "TalkerImpl!" );
    }
};

class MyAbstractClass
    : public virtual TalkerInterface
{
public:
    void foo() const { saySomething(); }
};

class MyClass
    : public MyAbstractClass
    , public TalkerImpl
{};

int main()
{
    MyClass().foo();
}

The virtual inheritance ensures that there is only one sub-object of type TalkerInterface in a MyClass instance. This has some counter-intuitive consequences. One is that "inheriting in an implementation" works, and another is that construction of that base class sub-object happens down in each MyClass constructor, and more generally down in the most derived class.

Cheers & hth.,

Share:
12,003
Rella
Author by

Rella

Hi! sorry - I am C/C++ noobe, and I am reading a book=)

Updated on June 04, 2022

Comments

  • Rella
    Rella almost 2 years

    Coluld you provide a simple code example? (sorry C++ nube) and how to call a function from the class you are extending?