Inheritance- intro
The following topics are covered in this section:
Inheritance
Inheritance means getting something from the parent. In C++
also, inheritance means the same. But who is the parent?
Remember that classes and objects are a key feature in C++.
When we talk of inheritance in C++, we refer to classes. We will have the parent
class and the derived class. The derived class inherits all properties of the
parent class. The parent class is also known as the base class. All classes that
arise from the base class are known as derived classes. These derived classes
inherit all non-private parts of the base class (i.e. there are a few things
that cannot be inherited from the base class. This is discussed later).
We have discussed earlier about a general class called
‘bacteria’. We also referred to two other classes called Ecoli and TB. Now,
bacteria will form the base class while ecoli and TB will be derived from the
bacteria class. In this way they will inherit all the general properties of a
bacteria. In addition they will have their own special features (in our case
they would have a special member function to describe the unique way in which
they move).
When any data is made private in a class, it can be accessed
ONLY by member functions of that class. A derived class CANNOT access the
private members of its base class.
It might seem as if the concept of data encapsulation would be lost if a derived class can access only it’s parent’s public members (because the aim of classes is to restrict access to data and hence we make them public. But if we can’t inherit the private data, what’s the use of inheritance?). C++ provides us with a third access specifier called ‘protected’.
The three access specifiers for class members are:It might seem as if the concept of data encapsulation would be lost if a derived class can access only it’s parent’s public members (because the aim of classes is to restrict access to data and hence we make them public. But if we can’t inherit the private data, what’s the use of inheritance?). C++ provides us with a third access specifier called ‘protected’.
- private
- public
- protected
If you declare class members as ‘protected’, then any class
derived from the parent can access the protected members but no one from outside
can make use of the protected members. In a way protected members are similar to
private class members (because both cannot be accessed from outside the class).
But both are different because protected members can be accessed by derived
classes whereas private members cannot be accessed by derived classes. The use
of protected access specifier is very important in inheritance.
Let's take a look at the syntax for making a derived class.
Create the base class as you normally would. Then when you want to create the derived class, use the following syntax:
{
//body of d1;
};
Let’s consider a different example for inheritance (instead
of bacteria and biology). Consider cars: there are different types of cars in
the world. Broadly we could divide them into two categories: normal cars and
racing cars. Both of these categories belong to the same family of cars (since
all cars will have some properties in common). Though they are common in certain
respects they will have their own properties as well. Thus these two categories
of cars can be derived from the general class of cars. Again within racing cars
we have different manufacturers (and each manufacturer might make use of
different specifications). For instance a ‘Ferrari’ and a ‘McLaren’ are not one
and the same (even though both are racing cars). Thus let’s model a general
class called ‘car’ (which will refer to racing cars). We will provide this class
with only two properties: color and speed. From this class, we shall derive two
classes called ‘Ferrari’ and ‘McLaren’.
From the above figure, it can be said that, “an object of
type Ferrari is a RacingCar”. This is referred to as “is-a”
relationship. The various types of relationships are:
When designing relationships between classes, ideally,
whatever the parent can do the child should be able to do (this is the ‘is-a’
relationship).
// Program to demonstrate inheritance
class car
{
int speed;
char color[20];
public:
car( )
{
cout<<"\nCreating a Car";
}
~car( )
{
cout<<"\nDestroying Car";
}
void input( )
{
cout<<"\n\nEnter the colour of your car : ";
cin>>color;
cout<<"\nEnter the top speed : ";
cin>>speed;
}
class ferrari : public car
{
int f;
public:
ferrari( )
{
cout<<"\nCreating a Ferrari";
}
~ferrari( )
{
cout<<"\nDestroying the Ferrari";
}
class mclaren : public car
{
private:
int m;
public:
mclaren( )
{
cout<<"\nCreating a McLaren";
}
~mclaren( )
{
cout<<"\nDestroying the McLaren";
}
int main( )
{
mine.input( );
return 0;
You might be wondering as to how the constructors and destructors will execute and in what order?
The output would be:
Creating a Ferrari
Enter the colour of your car : red
Enter the top speed : 150
Destroying the Ferrari
Destroying Car
As can be seen, constructors execute from the top to bottom
while the destructors execute in the reverse order. It’s like you will first
construct a car and then label it as a Ferrari and while destroying you would
remove the label Ferrari first and then dismantle the car.
Remember: The constructor of the base class and also the
destructor will be invoked.
More on Inheritance (Base-Class Access specifier)
So far we have used the following syntax for inheritance:
{
//body of class;
};
where derived-name is the name of the derived class and
base-name is the name of the base class.
We have already dealt with access specifiers as used for class members. The type of access specifier used determines how the members of a class are accessed (i.e. whether they can be accessed from outside, or whether their access is restricted to class members or whether they can be accessed by derived classes). The 3 types of access specifiers are:
We have already dealt with access specifiers as used for class members. The type of access specifier used determines how the members of a class are accessed (i.e. whether they can be accessed from outside, or whether their access is restricted to class members or whether they can be accessed by derived classes). The 3 types of access specifiers are:
- private
- protected
- public
class name-of-derived-class : access-specifier name-of-base-class
{
//body of derived class;
};
We know that the derived class cannot access the private
members of the base class. But what about the protected and public members of
the base class? Do they become private members of the derived class? It is for
clarifying this that we have to use an access-specifier to specify what we want
the inherited members to become.
- If you use the access specifier ‘public’:
All the public members of the base class become public members of the derived class. The protected members become protected members of the derived class. - When you use ‘private’ as the access specifier:
All public members of base class become private members of derived class.
All protected members of base class also become private members of derived class. - When the base class is accessed as ‘protected’:
All the public and protected members of base class become protected members of derived class.
No comments:
Post a Comment