Single-inheritance is where one class inherits directly from another class:
class A {};
class B : public A {};
Here, class B inherits all the public and protected members of class A.
Multiple-inheritance is where one class inherits directly from two or more classes:
class A {};
class B {};
class C : public A, public B {};
Here, class C inherits all the public and protected members of both A and B.
Multi-level inheritance is where one class inherits from another class that itself derived.
class A {};
class B : public A {};
class C : public B {};
Here, class B inherits all the public and protected members of A while class C inherits all the public and protected members of B, including those inherited from A.
Virtual inheritance applies to multi-level inheritance whereby a virtual base class becomes a direct ancestor to the most-derived class. This variation of inheritance is typically used in multiple inheritance situations where two or more intermediate classes inherit from the same base class:
class A {};
class B : public virtual A {};
class C : public virtual A {};
class D : public B, public C {};
Here, classes B and C both inherit from class A. Without virtual inheritance this would mean class D would inherit two instances of A (B::A and C::A), thus creating ambiguity when referring to D::A. By employing virtual inheritance, D inherits directly from A, and both B and C inherit from D::A. In other words, B and C share the same instance of A.
Another use of virtual inheritance is when you need to make a class final.
class A;
class B
{
friend class A;
B() {} // private constructor
};
class A : public virtual B
{
};
Here, class A is the final class. Class B is a helper class that has a private constructor while class A is declared a friend of class B. Class A is therefore the only class that can inherit from class B as it is the only class that can construct objects from class B. However, by inheriting class B virtually, we ensure that no other class can be derived from class A because virtual inheritance ensures that the most-derived class must be able to construct a class B object first. Currently, only class A has that privilege and must always be the most-derived class.
Yes.
C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick. For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.
struct A {}; // base class struct B : A {} // derived class (single inheritance).
It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.
They are not examples of languages. They arelanguages.
struct base1 { // ... }; struct base2 { // ... }; struct derived1 : public base1 // single inheritance { // ... }; struct derived2 : public base1, public base2 // multiple inheritance { // ... };
The concepts of OOP in C++ are the same as for OOP in any other programming language: abstraction, encapsulation, inheritance and polymorphism.
Are very useful. Examples: & | ^ ~
You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.
Examples: 1, -1, -2.5, 'a', "Hello", NULL
The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.
Because that's the way Java is designed. The designers felt that multiple inheritance was an unnecessary complication that offered little value so they did not include it in the JBC/JVM.