D Programming Inheritance - D Programming Language

What is D Programming Inheritance?

One of the maximum critical ideas in object-oriented programming is inheritance. Inheritance permits to outline a category in phrases of another class, which makes it easier to create and maintain an application. This additionally provides an opportunity to reuse the code functionality and fast implementation time.

When creating a class, in place of writing completely new statistics members and member functions, the programmer can designate that the new class needs to inherit the members of an existing class. This existing class is referred to as the base class, and the brand new class is referred to as the derived class.

The idea of inheritance implements the is a relationship. For instance, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as nicely and so on.

Base classes and Derived classes in D

A class may be derived from more than one class, which means that it could inherit information and features from multiple base training. To define a derived class, we use a category derivation list to specify the bottom class(es). a category derivation listing names one or extra base instructions and has the form –

class derived-class: base-class

Consider a base class Shape and its derived class Rectangle as follows −


When the above code is compiled and executed, it produces the following result −


Access Control and Inheritance

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

A derived class inherits all base class methods with the following exceptions −

Constructors, destructors, and copy constructors of the base class.
Overloaded operators of the base class.

Multi Level Inheritance

The inheritance can be of multiple levels and it is shown in the following example.

When the above code is compiled and executed, it produces the following result −

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

D Programming Language Topics