Inheritance
is the process by which new classes called derived classes are created from existing
classes called base classes. The derived classes
have all the features of the base class and the programmer can choose to add
new features specific to the newly created derived class.
For example, a programmer can create a base class named fruit and define derived classes as mango,orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.
This concept of Inheritance leads to the concept of polymorphism.
Features or Advantages of Inheritance:
Reusability:
Inheritance
helps the code to be reused in many situations. The base class is defined and
once it is compiled, it need not be reworked. Using the concept of inheritance,
the programmer can create as many derived classes from the base class as needed
while adding specific features to each derived class as needed.
Saves
Time and Effort:
The
above concept of reusability achieved by inheritance saves the programmer time
and effort. Since the main code written can be reused in various situations as
needed.
General Format for implementing the concept of Inheritance:
class
derived_classname: access specifier baseclassname
For
example, if the base class is exforsys and the derived class is sample it is
specified as:
class sample: public exforsys |
The
above makes sample have access to both public and protected variables of base class exforsys.
Reminder about public, private and protected access specifiers:
- If a member or variables defined in a
class is private, then they are accessible by members of the same class
only and cannot be accessed from outside the class.
- Public members and variables are
accessible from outside the class.
- Protected access specifier is a stage between private and public. If a member functions or variables defined in a class are protected, then they cannot be accessed from outside the class but can be accessed from the derived class.
Inheritance Example:
class exforsys { public: exforsys(void) { x=0; } void f(int n1) { x= n1*5; }
void output(void) { cout<<x; }
private:
int x; };
class sample: public exforsys
{ public: sample(void) { s1=0; }
void f1(int n1)
{ s1=n1*10; }
void output(void)
{ exforsys::output(); cout << s1; }
private:
int s1; };
int main(void)
{ sample s; s.f(10); s.output(); s.f1(20); s.output(); } |
The
output of the above program is
50
200
200
In the
above example, the derived class is sample and the base class is exforsys.
The derived class
defined above has access to all public and private variables. Derived classes cannot have access to base
class constructors and destructors. The derived
class would be able to add new member functions, or variables, or new
constructors or new destructors. In the above example, the derived class sample
has new member function f1( ) added in it. The line:
sample s; |
creates
a derived class object named as s. When this is created, space is allocated for
the data members inherited from the base class exforsys and space is additionally allocated
for the data members defined in the derived class sample.
The base class constructor exforsys is used to initialize the base class
data members and the derivedclass constructor sample is used to initialize the data
members defined in derived class.
No comments:
Write comments