Object Oriented Programming - C++
Inheritance - C++
Inheritance is the ability of a class to inherit features of one or
more other classes. That is, inheritance is achieved by creating a new or
derived class from at least one exisiting class. The existing class
sometimes is called the base class, and the new class is called the
derived class.
Inheritance pertains to class, and not to ordinary non-class variables
and functions. In practice programs do inherit. For example, programs
inherit windows, screens, and such gadgets. In an integrated environment
the main screen for a Spreadsheet is thesame across the board for the
Wordprocessor and the Database programs. This comes as result of one
application inheriting features that they have in common.
Charateristics of Inheritance
- Base class - the class from which a new class is derived
- The derived class inherits members from the base class
- The derived class may add to its inheritance
- Private members of the base class remains private to the
base class
- Added functions of the derived class cannot have access to
any of the private members of the base class
- Private members of the base class may be accessed by member
functions that were inherited from the base class
Base and Derived Classes
As was mentioned, inheritance is achieved by creating a new or a derived
class from at least one exisiting class. The format for class derivation
follows the following pattern:
class Derived: access_specifier Base1, access_specifier Base2,
….
{
// Code for the derived class
};
Access specifier can either be:
- private, where private members that are
derived from the base class remains private to the base class
- protected, where members of the derived
class can have access to protected members of the base class, or
- public, where members of the derived class
can have access to public members of the base class.
Analysis
- If a derived class needs to access a private data member
of the base class, change that private member to a protected member of
the base class.
- Neither main nor any non-member function can have access
to either private members or protected members·
- There can be multiple inheritances from a base class
- There can be heirarchy of inheritances
- Constructors and destructors are not inherited
- Each derived class must provide its own constructors and
destructors
- The order in which the constructors are called match the
order in which they are specified in the derived class' inheritance list.
- The destructors are called in the reverse order of how
the constructors were called
- A derived class' constructor arguments can be passed to
base class constructor
- Demonstrations Programs
- Inheritance I: Introduction
- Inheritance II:Private vs Public
Members
- Inheritance III Protected Members
- Inheritance IV Constructors and
Destructors
- Inheritance IV class: A Data Type
- Inheritance IV Derived class
initializes Base class constructor
- Inheritance I: Go Back to the
Schedule