Concept Of Inheritance
The concept of inheritance is one of the key features of an object-oriented programming language. Inheritance allows a programmer to define a general class, and then later define more specific classes that share or inherit all of the properties of the more general class.
Inheritance is one of the feature of Object Oriented Programming System(OOPs), it allows the child class to acquire the properties (the data members) and functionality (the member functions) of parent class. A class that inherits another class is known as child class, it is also known as derived class or subclass.
What is child class?
A class that inherits another class is known as child class, it is also known as derived class or subclass.
What is parent class?
The class that is being inherited by other class is known as parent class, super class or base class.
A class that inherits another class is known as child class, it is also known as derived class or subclass.
What is parent class?
The class that is being inherited by other class is known as parent class, super class or base class.
Syntax of Inheritance
class parent_class { //Body of parent class }; class child_class : access_modifier parent_class { //Body of child class };
What are the advantages of using inheritance in C++ Programming
The main advantages of inheritance are code reusability and readability. When child class inherits the properties and functionality of parent class, we need not to write the same code again in child class. This makes it easier to reuse the code, makes us write the less code and the code becomes much more readable.
Lets take a real life example to understand this: Lets assume that
Now we want to create
This approach makes us write less code as both the classes inherited several properties and functions from base class thus we didn’t need to re-write them. Also, this makes it easier to read the code.
Human is a class that has properties such as height, weight, color etc and functionality such as eating(), sleeping(), dreaming(), working() etc.Now we want to create
Male and Female class, these classes are different but since both Male and Female are humans they share some common properties and behaviours (functionality) so they can inherit those properties and functionality from Human class and rest can be written in their class separately.This approach makes us write less code as both the classes inherited several properties and functions from base class thus we didn’t need to re-write them. Also, this makes it easier to read the code.
Comments
Post a Comment