answersLogoWhite

0


Best Answer

There's no such thing as hybrid inheritance in C++. Hybrid inheritance implies two or more different types of inheritance but there are really only two types of inheritance in C++ and they are mutually exclusive: single inheritance and multiple inheritance.

A class that inherits directly from one class uses single inheritance.

A class that inherits directly from two or more classes uses multiple inheritance.

The only way to combine these two inheritance patterns is through multi-level inheritance, where a class inherits directly from one or more derived classes. However, whenever we create a derivative, we're only concerned with the base class or classes we are directly inheriting from. The fact they may or may not be derivatives themselves is largely irrelevant from the viewpoint of the derivative. Indeed, the only time we really need to consider one of the lower bases classes is when we need to explicitly invoke a virtual function of that particular class, as opposed to implicitly invoking the most-derived override of that function as we normally would. However, this is really no different to a derived class override invoking its direct base class method.

Virtual base classes are also thought of as being a type of hybrid inheritance, however virtual base classes merely determine which class is responsible for the construction of those classes. Normally, a derived class is responsible for the construction of all its direct base classes, which must be constructed before the derived class can begin construction. In turn, those base classes are responsible for the construction for their own base classes. In this way, derived classes are automatically constructed from the ground up, base classes before derived classes, in the order declared by the derived class.

For example, consider the following hierarchy:

struct X {};

struct Y : X {};

struct Z : Y {};

Z inherits from Y so in order for a Z to exist we must first construct a Y. By the same token, Y inherits from X so in order for a Y to exist we must first construct an X. Thus when we initiate construction of a Z, that initiates construction of a Y which initiates construction of an X.

Now consider a virtual base class:

struct X {};

struct Y : virtual X {};

struct Z : Y {};

The construction sequence is exactly the same as before (X before Y before Z), the only difference is that when we now instantiate a Z, as the most-derived class in the hierarchy it becomes responsible for the construction of the virtual X. Z is also (still) responsible for the construction of a Y, but Y no longer needs to construct an X because a (virtual) X already exists.

Virtual base classes become more relevant in multiple inheritance, where two or more base classes share a common base class:

struct W {};

struct X : virtual W {};

struct Y : virtual W {};

struct Z : X, Y {};

Here, Z uses multiple inheritance from X and Y. Both X and Y use single inheritance from W. Without virtual inheritance, Z would inherit two separate instances of W, specifically X::W and Y::W. But by declaring W as a virtual base of X and Y, the most-derived class, Z, becomes responsible for the construction of W, as well as its direct base classes, X and Y. Neither X nor Y need to construct a W because a W will already exist. Thus X::W and Y::W now refer to the same instance of W.

Note that we do not need to write any additional code for this mechanism to work. The virtual keyword alone is all we need. Even if X or Y provided explicit initialisation of W, those initialisers would be ignored by the compiler since initialisation of W is automatically the responsibility of the most-derived class. The only time those explicit initialisers would be invoked is if we explicitly instantiate an instance of X or Y, because then X or Y become the most-derived class.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Hybrid inheritance basically means any combination of the four basic inheritance models: single, multiple, hierarchical and multi-level inheritance.

Single inheritance applies wherever one class is derived directly from another base class.

Multiple inheritance applies wherever one class is derived directly from two or more base classes.

Hierarchical inheritance applies wherever two or more classes are derived from the same base class.

Multi-level inheritance applies wherever one class is derived from a base class that is itself derived from another base class.

Hybrid inheritance can use any combination of these four hierarchies. A typical example uses four classes, A, B, C and D, as follows:

struct A {};

struct B : A {};

struct C : A {};

struct D : B, C {};

We're using the struct keyword rather than the class keyword simply because a struct has public access by default. However the same principals apply to both since a struct is a class by another name. The only real difference between the two is that a class has private access by default.

Class A is a base class and is the least-derived class in the hierarchy while class D is the most-derived class in the hierarchy.

Class B inherits directly from A and is therefore using single inheritance. Class C also inherits from A using single inheritance. However, since B and C both inherit directly from A, this is also an example of hierarchical inheritance. Thus even without considering D, these three classes, between them, are using hybrid inheritance.

Class D employs multiple inheritance to derive from both B and C. Since B and C are both derived from A, this is also an example of multi-level inheritance. Thus A, B, C and D, between them, are using all four types of inheritance.

The main problem with this hybrid hierarchy is that D inevitably inherits two instances of A (D::B::A and D::C::A). This creates an ambiguity whenever we implicitly access D::A as the compiler has no way of knowing if we meant D::B::A or D::C::A. Provided we're explicit this isn't a major problem but there is a much better way.

If B and C could share the same instance of A we would not only eliminate the ambiguity we could also reduce the memory footprint of class D. To achieve this we must use another type of inheritance called virtual inheritance. Note that virtual inheritance is often confused with hybrid inheritance, but while you can have hybrid inheritance without virtual inheritance (as already shown), you cannot have virtual inheritance without hybrid inheritance.

To understand how virtual inheritance works you first need to understand the order in which objects are constructed without virtual inheritance.

Whenever we instantiate an instance of D, we are actually invoking a complex chain reaction of constructors. That is, in order to instantiate D we must first instantiate B and C. But in order to instantiate B we must first instantiate B::A. Similarly, in order to instantiate C we must first instantiate C::A. Thus the order of construction is really B::A, then B, followed by C::A then C, and finally D. All this is done automatically for us through implied initialisation sections in each class constructor. However we can explicitly declare our own initialisation sections to give us greater control over construction. The following therefore emulates the default behaviour of the default constructors:

struct A {};

struct B : A { B() : A() {}};

struct C : A { C() : A() {}};

struct D : B, C { D() : B(), C() {}};

As we can see, D() invokes B() followed by C(). However, C() can't be invoked until after B() has invoked A(). It should be noted that no valid object actually exists until we fully return from each constructor's body, which comes immediately after each class is initialised. Thus D does not physically exist until both B and C return from their construction bodies. More importantly, if any base class should fail to construct for any reason, the entire hierarchy fails to construct, and any base classes that succeeded up to the point of failure will automatically cease to exist. With static objects this is rarely a problem because construction failure would result in a compile time error. But with dynamic objects it's vital to ensure the return value from the new operator is non NULL before using the object, otherwise a runtime error will occur.

Now that we know how objects are constructed through normal inheritance let's examine how virtual inheritance works.

struct A {};

struct B : virtual A { B() : A() {}};

struct C : virtual A { C() : A() {}};

struct D : B, C { D() : B(), C() {}};

Here we've declared the exact same classes as before but we've included the keyword virtual. Note that the public, protected or private keyword, if required, may be placed before or after the virtual keyword, it makes no difference. In this case public inheritance is implied since we're using the struct keyword rather than the class keyword but we could have written public virtual or virtual public -- they would both mean exactly the same thing.

In this case, A is the virtual base class. When declaring virtual base classes, we must be sure to make the declaration within all derived classes that inherit directly from it (the hierarchical inheritance model), unless you can be absolutely certain that a derived class would never be used with multiple inheritance or where the derived class must retain its own instance of the base class. In this case, both B and C inherit from A therefore both declare A as a virtual base class.

What this means is that the most-derived class is now responsible for construction of the virtual class, just as if it had inherited directly from that class. This does not affect the construction of B or C objects in their own right since both would be the most-derived class in their individual hierarchies. But when we construct a D object, we alter the construction sequence. Instead of B::A, B, C::A, C then D, we now have the much simpler sequence: A, B, C then D.

To put it another way, instead of B::A and C::A being separate instances of A, they are in fact the same instance of A. B::A and C::A are really nothing more than references or aliases to this one instance. You can also refer to this instance as D::A. With only one instance of A there is no longer any ambiguity about which instance is implied when referring to A.

Not all classes can make use of virtual inheritance. For instance, if A had a virtual method and both B and C override that same method, then the compiler cannot determine which of the two overrides should be invoked unless you also override that method in D (in which case D's override would be invoked).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus programming language program for hybrid inheritance?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Does hybrid inheritance consist of ANY two types of inheritance?

There are only two types of inheritance to begin with: single inheritance and multiple inheritance. Since they are mutually exclusive there is no such thing as hybrid inheritance.


What are the three general methods in implementing a programming language?

1- Compilation 2- Pure Interpretation 3- Hybrid Implementation System


Why high bred inheritance not use in C sharp?

It was the designer's decision. One would have asked the similar question if it had been design the other way. It would not be fun with another hybrid, in inheritance design, language just like C++.


What level language is c?

Hi, As we know, computer languages are mainly of three types: a) Low level languages b) High Level languages c) Hybrid languages As 'C' has all powers of first two types, i.e., you can program a system's BIOS using Assembly code in 'C' and could write general programs. So, its an hybrid language, a combination of both.


What is pure object oriented language?

Object-oriented (OO) applications can be written in either conventional languages or OOPLs, but they are much easier to write in languages especially designed for OO programming. OO language experts divide OOPLs into two categories, hybrid languages and pure OO languages. Hybrid languages are based on some non-OO model that has been enhanced with OO concepts. C++ (a superset of C), Ada 95, and CLOS (an object-enhanced version of LISP) are hybrid languages. Pure OO languages are based entirely on OO principles; Smalltalk, Eiffel, Java, and Simula are pure OO languages.Reference: Tokar, Joyce L. "Ada 95: The Language for the 90's and Beyond."" According to me JAVA is not a pure oop Language ,because java contains primitive datatypes that's not an Objects."SmalltalkEiffeljavaa programming language that includes all the oops concepts i,e object, class , inheritance,abstraction, encapsulation, data binding, and message passing is called a completely object oriented programming.. example:java.

Related questions

What types of inheritance in Java programming?

Java uses a hybrid system of inheritance. The designers chose a compromise between strict single inheritance and full multiple inheritance.See the related questions section below for more information.


Does hybrid inheritance consist of ANY two types of inheritance?

There are only two types of inheritance to begin with: single inheritance and multiple inheritance. Since they are mutually exclusive there is no such thing as hybrid inheritance.


What are the three general methods in implementing a programming language?

1- Compilation 2- Pure Interpretation 3- Hybrid Implementation System


Why high bred inheritance not use in C sharp?

It was the designer's decision. One would have asked the similar question if it had been design the other way. It would not be fun with another hybrid, in inheritance design, language just like C++.


What has the author Wayne Allan Walker written?

Wayne Allan Walker has written: 'Hybrid trees as a data structure' -- subject(s): Hybrid computers, SNOBOL (Computer program language)


What kind of inheritance is not allowed in java?

Java does not allow the multiple inheritance of concrete classes, though it does allow a "hybrid" inheritance of one concrete class and multiple interfaces.


The machine language and assembly language for each CPU architecture are the highest-level programming languages?

No, it is just the opposite assembly language is the lowest level of programming language.A high level language uses one command to do complicated things such as placing buttons on the screen or triggering events when you click the mouse. Low level programming like assembly programming takes many lines of code to do even the most simple things such as putting text on the screen.Low level programming gives the programmer more fine machine specific control, however with machine specific control you loose the ability to run your programs on different computers. The best bet for programming is to use a hybrid language such as C++ or Basic. They have the ability to use low level programming, but with the use of library extensions you can also use high level code.Hopefully this is helpful to you. :)


What type of inheritance when the genotype is neither homozygous or heterozygous?

Hybrid, probably. That means it would be unable to reproduce.


What are the 3 general methods of implementing programming language in brief?

1- Compilation: * Programs can be translated into machine language. * Very fast program execution, once the translation process is complete. * Most production implementations of language such as C,COBOL,and Ada are by compiler. 2- Pure Interpretation: * Has the Advantage of allowing easy implementation of many source-level debugging operations. * The serious disavantage the execution is 10 to 100 times slower than in compiled systems. 3- Hybrid Implementation System: * Small translation cost. * Medium execution speed. From this book : Concepts of Programming Languages (7th Edition) Robert W.Sebesta


Type of inheritances in c plus plus?

Single, multiple, multi-level, hierarchical and hybrid/virtual inheritance. Single inheritance applies when one class inherits from just one base class. Multiple inheritance applies when one class inherits from two or more base classes. Multi-level inheritance applies to a class that inherits from at least one base class that is itself derived from another base class. Hierarchical inheritance applies to a base class that is inherited by two or more separate derived classes. Hybrid inheritance combines multiple inheritance, multi-level inheritance and hierarchical inheritance. That is, where A is a common base class of derived classes B and C, and B and C are both base classes of derived class D. Hybrid inheritance is often used with virtual inheritance where B and C inherit from A virtually rather than directly. In these cases, the virtual base class is instantiated by the most-derived class in the hierarchy, D, and this instance is then shared by both B and C.


What level language is c?

Hi, As we know, computer languages are mainly of three types: a) Low level languages b) High Level languages c) Hybrid languages As 'C' has all powers of first two types, i.e., you can program a system's BIOS using Assembly code in 'C' and could write general programs. So, its an hybrid language, a combination of both.


What is pure object oriented language?

Object-oriented (OO) applications can be written in either conventional languages or OOPLs, but they are much easier to write in languages especially designed for OO programming. OO language experts divide OOPLs into two categories, hybrid languages and pure OO languages. Hybrid languages are based on some non-OO model that has been enhanced with OO concepts. C++ (a superset of C), Ada 95, and CLOS (an object-enhanced version of LISP) are hybrid languages. Pure OO languages are based entirely on OO principles; Smalltalk, Eiffel, Java, and Simula are pure OO languages.Reference: Tokar, Joyce L. "Ada 95: The Language for the 90's and Beyond."" According to me JAVA is not a pure oop Language ,because java contains primitive datatypes that's not an Objects."SmalltalkEiffeljavaa programming language that includes all the oops concepts i,e object, class , inheritance,abstraction, encapsulation, data binding, and message passing is called a completely object oriented programming.. example:java.