Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused on C++ multiple inheritance

I'm somewhat new to the more advanced features of C++. Yesterday, I posted the following question and I learned about virtual inheritance and the dreaded diamond of death.

Inheriting from both an interface and an implementation C++

I also learned, through other links, that multiple inheritance is typically a sign of a bad code design and that the same results can usually be better achieved without using MI. The question is... I don't know what is a better, single-inheritance approach for the following problem.

I want to define an Interface for two types of Digital Points. An Input Digital Point and an Output Digital Point. The Interface is to be slim, with only what's required to access the information. Of course, the vast majority of properties are common to both types of digital points. So to me, this is a clear case of Inheritance, not Composition.

My Interface Definitions look something like this:

// Interface Definitions
class IDigitalPoint
{
public:
  virtual void CommonDigitalMethod1() = 0;
};

class IDigitalInputPoint : virtual IDigitalPoint
{
public:
  virtual void DigitialInputMethod1() = 0;
};

class IDigitalOutputPoint : virtual IDigitalPoint
{
public:
  virtual void DigitialOutputMethod1() = 0;
};

My implementations look like this:

// Implementation of IDigitalPoint
class DigitalPoint : virtual public IDigitalPoint
{
public:
  void CommonDigitalMethod1();
  void ExtraCommonDigitalMethod2();
}

// Implementation of IDigitalInputPoint
class DigitalInputPoint : public DigitalPoint, public IDigitalInputPoint 
{
public:
  void DigitialInputMethod1();
  void ExtraDigitialInputMethod2();
}

// Implementation of IDigitalOutputPoint
class DigitalOutputPoint : public DigitalPoint, public IDigitalOutputPoint 
{
public:
  void DigitialOutputMethod1();
  void ExtraDigitialOutputMethod2();
}

So how could I reformat this structure, to avoid MI?

like image 875
ThermoX Avatar asked Jun 22 '16 10:06

ThermoX


People also ask

What is the common mistake in using multiple inheritance?

The error is due to the “Diamond Problem” of multiple inheritance. The Snake class does not know which breathe() method to call. In the first example, only the Animal class had overridden the breathe() method.

Does C allow multiple inheritance?

Master C and Embedded C Programming- Learn as you go So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.

Why multiple inheritance is not possible in C sharp?

C# compiler is designed not to support multiple inheritence because it causes ambiguity of methods from different base class. This is Cause by diamond Shape problems of two classes If two classes B and C inherit from A, and class D inherits from both B and C.

How do you avoid the diamond problem in multiple inheritance?

The solution to the diamond problem is default methods and interfaces. We can achieve multiple inheritance by using these two things. The default method is similar to the abstract method. The only difference is that it is defined inside the interfaces with the default implementation.


1 Answers

"multiple inheritance is typically a sign of a bad code design" - parents that are pure interfaces are not counted in regards to this rule. Your I* classes are pure interfaces (only contain pure virtual functions) so you Digital*Point classes are OK in this respect

like image 124
mvidelgauz Avatar answered Oct 21 '22 01:10

mvidelgauz