Inheritance in C++.

Inheritance

  • In C++, inheritance is a mechanism where one object naturally acquires all of the properties of its parent class.
  • In such a way that the attributes and behaviors specified in another class can be reused, expanded or modified.
  • 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.
  • In C++, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
  1. Base class (parent) - the class being inherited from.
  2. Derived class (child) - the class that inherits from another class.
  • The process that allows one to expand a class description without making any physical changes to the current class is inheritance.
  • Heritage helps you to build new classes out of existing classes. Any new class you build from an existing class is referred to as derivative class; the original class is called base class.
  • The inheritance relation allows a derived class to inherit characteristics from its base class. 
  • The derived class can additionally add new features of its own.
  •  So instead of building entirely new classes from scratch, you can take advantage of inheritance and reduce the sophistication of the program.

Let us take an example

class Person 
{
  ... .. ...
};
class EnglishTeacher : public Person 
{
  ... .. ...
};
class Footballer : public Person
{
  .... .. ...
};
  • In the above example, class Person is a base class and classes EnglishTeacher and Footballer are the derived from Person.
  • The derived class appears with the declaration of a class followed by a colon, the keyword public and the name of base class from which it is derived.
  • Since, EnglishTeacher and Footballer are derived from Person, all data member and member function of Person can be accessible from them.


Types of Inheritance

  1. Single Inheritance: It is the in which a derived class is inherited from the only one base class.
  2. Multiple Inheritance: It is the process of deriving a new class that inherits the attributes from two or more classes.
  3. Multilevel Inheritance: It is the process of deriving a class from another derived class.
  4. Hierarchical Inheritance: It is the process of deriving more than one class from a base class.
  5. Hybrid Inheritance: Hybrid inheritance is the process of combining of more than one type of inheritance.
Note:- In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format...
class derived_class: memberAccessSpecifier base_class
{ ...
};
  • Where derived_class is the name of the derived class and base_class is the name of the class on which it is based. 
  • The member Access Specifier may be public, protected or private. 
  • This access specifier describes the access level for the members that are inherited from the base class.
Member Access Specifier
How Members of the Base Class Appear in the Derived Class
Private
Private members of the base class are inaccessible to the derived class.
Protected members of the base class become private members of the derived class.
Public members of the base class become private members of the derived class.
Protected
Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class.
Public members of the base class become protected members of the derived class.
Public
Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class.
Public members of the base class become public members of the derived class.

In principle, a derived class inherits every member of a base class except constructor and destructor. It means private members are also become members of derived class. But they are inaccessible by the members of derived class.
//Program to implement Inheritance in C++ Programming.

#include <iostream.h>
#include <conio.h>
class Person
{
     public:
        string profession;
        int age;
        Person(): profession("unemployed"), age(16) { }
        void display()
        {
             cout << "My profession is: " << profession << endl;
             cout << "My age is: " << age << endl;
             walk();
             talk();
        }
        void walk() { cout << "I can walk." << endl; }
        void talk() { cout << "I can talk." << endl; }
};
// EnglishTeacher class is derived from base class Person.
class EnglishTeacher : public Person
{
    public:
       void teachMaths() { cout << "I can teach Maths." << endl; }
};
// Footballer class is derived from base class Person.
class Footballer : public Person
{
    public:
       void playFootball() { cout << "I can play Football." << endl; }
};
int main()
{
     EnglishTeacher teacher;
     teacher.profession = "Teacher";
     teacher.age = 23;
     teacher.display();
     teacher.teachMaths();

     Footballer footballer;
     footballer.profession = "Footballer";
     footballer.age = 19;
     footballer.display();
     footballer.playFootball();

     return 0;
}

Expected Output:-

My profession is: Teacher
My age is: 23
I can walk.
I can talk.
I can teach Maths.
My profession is: Footballer
My age is: 19
I can walk.
I can talk.
I can play Football.
//Following example explains concept of inheritance 
class Shape
{
float width, height;
protected:
void set_data (float a, float b)
public: {
height = b;
width = a; } };
class Rectangle: public Shape
{ public: float area () {
class Triangle: public Shape
return (width * height); } }; { public:
float area () { return (width * height / 2); } };
rect.set_data (5,3);
int main () { Rectangle rect; Triangle tri; tri.set_data (2,5);
return 0;
cout << rect.area() << endl; cout << tri.area() << endl; } 

Expected Output :-
15
5
set_data () and area() are public members of derived class and can be accessed from outside class i.e. from main()The object of the class Rectangle contains :
width, height inherited from Shape becomes the protected member of Rectangle.
set_data() inherited from Shape becomes the public member of Rectangle
area is Rectangle’s own public member

The object of the class Triangle contains :
width, height inherited from Shape becomes the protected member of Triangle.
set_data() inherited from Shape becomes the public member of Triangle
area is Triangle’s own public member

Post a Comment

Previous Post Next Post