C++ Inheritance
C++ INHERITANCE
Inheritance is one of the key features of Object-oriented programming in C++. It allows user to create a new class (derived class) from an existing class (base class).
The derived class inherits all the features from the base class and can have additional features of its own.
Why inheritance should be used?
Suppose in your game, you want three characters - a maths teacher, a footballer, and a businessman.
Since all of the characters are persons, they can walk and talk. However, they also have some special skills. A maths teacher can teach maths, a footballer can play football and a businessman can run a business.
You can individually create three classes who can walk, talk and perform their special skill as shown in the figure below.
In each of the classes, you would be copying the same code for a walk and talk for each character.
If you want to add a new feature-eat, you need to implement the same code for each character. This can easily become error prone (when copying) and duplicate codes.
It'd be a lot easier if we had a Person class with basic features like talk, walk, eat sleep, and add special skills to those features as per our characters, This is done using inheritance,
Using inheritance, new you don,t implement the same code for the walk and talk for each class. You just need to inherit them.
So, for Maths teacher (derived class), You inherit all feature os a Person (base class ) and add a new feature Teach Maths. Likewise., for a footballer, you inherit all the features of a person and add a new feature Play Football and so on.
This makes your code cleaner, understandable and extendable.
In above ex. , class Person is a base class and classes Maths Teacher 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 name of a base class from which it is derived.
Since Maths Teacher and Footballer are derived from Person, all data member and a member function of Person can be accessible from them.
Suppose, a base class and derived class has a member function with same name and arguments.
If you create an object of the derived class and try to access that member function, the function in derived is only invoked.
The member function of derived class overrides the member function of the base class.
If you want to add a new feature-eat, you need to implement the same code for each character. This can easily become error prone (when copying) and duplicate codes.
It'd be a lot easier if we had a Person class with basic features like talk, walk, eat sleep, and add special skills to those features as per our characters, This is done using inheritance,
Using inheritance, new you don,t implement the same code for the walk and talk for each class. You just need to inherit them.
So, for Maths teacher (derived class), You inherit all feature os a Person (base class ) and add a new feature Teach Maths. Likewise., for a footballer, you inherit all the features of a person and add a new feature Play Football and so on.
This makes your code cleaner, understandable and extendable.
Implementation of inheritance in C++ Programming:
In above ex. , class Person is a base class and classes Maths Teacher 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 name of a base class from which it is derived.
Since Maths Teacher and Footballer are derived from Person, all data member and a member function of Person can be accessible from them.
Access specifiers in Inheritance:
When creating a derived class from a base class, you can use different access specifiers to inherit the data member of the base class.
These can be public, protected or private.
in the above example, the base class Person has been inherited public-ly by MathsTeacher and Footballer.
Member Function Overriding Inheritance:
If you create an object of the derived class and try to access that member function, the function in derived is only invoked.
The member function of derived class overrides the member function of the base class.
0 comments:
Post a Comment