answersLogoWhite

0

Examples of inheritance in c plus plus?

Updated: 8/18/2019
User Avatar

Wiki User

10y ago

Best Answer

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.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Examples of inheritance in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does c plus plus supports hierarchical inheritance?

Yes.


How ploymorphism and inheritance is different from that in Java and c plus plus?

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.


Write a c plus plus programme to illustrate single inheritance?

struct A {}; // base class struct B : A {} // derived class (single inheritance).


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


C plus plus and java are examples of what languages?

They are not examples of languages. They arelanguages.


Demonstrate single inheritance in C plus plus?

struct base1 { // ... }; struct base2 { // ... }; struct derived1 : public base1 // single inheritance { // ... }; struct derived2 : public base1, public base2 // multiple inheritance { // ... };


What are the concepts of object oriented programming in c plus plus?

The concepts of OOP in C++ are the same as for OOP in any other programming language: abstraction, encapsulation, inheritance and polymorphism.


C plus plus bit-wise operators?

Are very useful. Examples: & | ^ ~


How do you implement inheritance in c plus plus?

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.


What is literal in c plus plus?

Examples: 1, -1, -2.5, 'a', "Hello", NULL


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.


Why not java allowed multiple inheritance and c plus plus allowed?

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.