Inheritance Extending Classes: Defining Derived Class, Multiple Inheritance and Visibility Modes

Get unlimited access to the best preparation resource for CBSE/Class-7 Science: fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 179K)

Inheritance

Diagram of Base Class and Derived Class

Defining Derived Class

A derived class is defined by specifying its relationship with the base class using visibility mode.

The general form of defining a derived class is:

class derived _class: visibility _mode base _class

{

_________________

_________________ // members of derived class.

};

The colon indicates that the derived _class is derived (inherits some property) from base _class.

  • The base class(es) name(s) follow(s) the colon (:). The names of all the base classes of a derived class follow: (colon) and are separated by comma.

  • The visibility-mode can be either private or public or protected. If no visibility mode is specified, then by default the visibility mode is considered as private.

Following are some examples of derived class definitions:

class Marksheet: public student / / public derivation

{

// members of derived class

};

class Marksheet: private student / / private derivation

// members of derived class

};

class Marksheet: protected student // protected derivation

{

// members of protected class

};

  • In the above definitions, Marksheet is the derived class of student base class. The visibility mode public indicates that student is a public base class. Similarly, the visibility modes private or protected indicate that student is private base class or protected base class respectively.

  • When we say that members of a class are inheritable, it means that the derived class can access them directly.

  • However, the derived class has access privilege only to the non-private members of the base class.

  • Although the private members of the base class cannot be accessed directly, yet the objects of derived class are able to access them through the non-private inherited members.

Multiple Inheritance

As we know that a subclass inheriting from multiple base classes is known as multiple inheritances. The Syntax of defining a derived class is given below:

class derived _class name: mode baseclass1, mode baseclass2

{

// members of derived class

};

Example 1

class marks: public semester1, private semester2

{

// members

};

Visibility Modes

It can be public, private, or protected. The private data of base class cannot be inherited.

(i) If inheritance is done in public mode, public members of the base class become the public members of derived class and protected members of base class become the protected members of derived class.

(ii) In inheritances is done in a private mode, public and protected members of base class become the private members of derived class.

(iii) If inheritance is done in a protected mode, public and protected members of base class become the protected members of derived class.

The following table shows the three types of inheritance:

Image of Visibility Modes

Image of Visibility Modes

Developed by: