Sparrow: Inheritance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Inheritance

The capability of a class to derive properties and characteristics from another


class is called Inheritance. In C++, inheritance is a process in which one object
acquires all the properties and behaviors of its parent object automatically. In
such way, you can reuse, extend or modify the attributes and behaviors which
are defined in other class. In C++, the class which inherits the members of
another class is called derived class and the class whose members are inherited
is called base class. The derived class is the specialized class for the base class.

 derived class (child) - the class that inherits from another class


 base class (parent) - the class being inherited from

The idea of inheritance implements “  is a”  relationship.

For example, parrot IS-A bird.

Sparrow
They have grey head

To inherit from a class, use the : symbol.

class derived_class: access_specifier Base class

Where access-specifier is one of public, Protected, or private, and base-class is


the name of a previously defined class. If the access-specifier is not used, then it
is private by default.

class B : public A
where class B inherit the property of class A

A class can be derived from more than one classes, which means it can inherit
data and functions from multiple base classes.
We can summarize the data and member function of a class according to their
access specification- who can access them in the following way

Access Public Protected private

Same class Yes Yes yes

Derived classes Yes Yes no

Outside classes Yes No no

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.

 The friend functions of the base class.

Modes of Inheritance
We hardly use protected or private inheritance, but public inheritance is
commonly used. A base class's private members are never accessible directly
from a derived class, but can be accessed through calls to
the public and protected members of the base class.

While using different type of inheritance according to access specifier,


following rules are applied −

Public Inheritance − When deriving a class from a public base


class, public members of the base class become public members of the derived
class and protected members of the base class become protected members of the
derived class.

Protected Inheritance − When deriving from a protected base class, public and


protected members of the base class become protected members of the derived
class.
Private Inheritance − When deriving from a private base class, public and
protected members of the base class become private members of the derived
class.

Rules of Derivations

Base class Base Class Data and Function


access specifier Public Protected Private
Modes of inheritance
Public Public Protected Not accessible in
derived class
Protected Protected Protected Not accessible in
derived class
Private Private Private Not accessible in
derived class

You might also like