NIOS Computer Science: Chapter 14 – Inheritance Extending Classes Part 6

Download PDF of This Page (Size: 379K)

Image of Abstration example

Image of Abstration Example

Image of Abstration example

Abstract Class

An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class (to be inherited by other classes). It is a design concept in program development and provides a base upon which other classes may be built. In the previous example, the class student is an abstract class since it was not used to create any object.

Virtual Base Class

Consider a situation where all the three kinds of inheritance, namely, multilevel, multiple and hierarchical instances are involved. Consider the following example illustrated in a figure.

Image of Virtual Base Class

Image of Virtual Base Class

Image of Virtual Base Class

The child has two direct base classes fees and academics which themselves have a common base class ‘school’. The child inherits the traits of ‘school’ via two separate paths. It can also inherit directly as shown by the broken line. The ‘school’ is sometimes referred to as indirect base class. All the public and protected members of ‘school’ are inherited into ‘child’ twice, first via ‘fees’ and again via ‘academics’. This means, ‘child’ would have duplicate sets of the members inherited from ‘school’. This introduces ambiguity and should be avoided.

The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual class by declaring the base class as shown below :

class school

{

--------------

--------------

};

class fees : virtual public school

{

---------------

---------------

};

class academics : public virtual school

{

-------------------

--------------------

};

class child : public fees, public academics

{

/ /only one copy of school will be inherited.

};