Page 176 - DCAP404 _Object Oriented Programming
P. 176
Unit 9: Inheritance
tested, it can be adopted by other programmers. New classes can be defined reusing the properties Notes
of existing ones.
The mechanism of deriving a new class from an old one is called ‘INHERITANCE’. This is often
referred to as ‘IS-A’ relationship because every object of the class being defined “is” also an
object of inherited class type. The old class is called ‘BASE’ class and the new one is called
‘DERIVED’ class or sub-class.
9.1 Defining Derived Class
A derived class is specified by defining its relationship with the base class in addition to its own
details. The general syntax of defining a derived class is as follows:
class derivedclassname : access_specifier baseclassname
{
……
…… // members of derivedclass
};
The colon (:) indicates that the derivedclassname class is derived from the baseclassname class.
The access_specifier or the visibility mode is optional and, if present, may be public, private or
protected. By default it is private. Visibility mode describes the accessibility status of derived
features. For example,
class xyz //base class
{
//members of xyz
};
class ABC: public xyz //public derivation
{
//members of ABC
};
class ABC : XYZ //private derivation (by default)
{
//members of ABC
};
In inheritance, some of the base class data elements and member functions are inherited into the
derived class and some are not. We can add our own data and member functions and thus extend
the functionality of the base class.
Notes Inheritance, when used to modify and extend the capabilities of the existing classes,
becomes a very powerful tool for incremental program development.
LOVELY PROFESSIONAL UNIVERSITY 169