Inheritance in C++ Language(part 1)
Inheritance in C++ Language
First of all, why do we need inheritance:
We remember some of our earlier concept i.e
Class is used to describe properties and behaviour of an object.
Property's names and values
Behaviour means actions
Lets us assume that we want to represent a car.
As we are looking at quotation above,We Created a Car's Class in which we have made some properties i.e variable and have some methods i.e function to access the property or variable through
object.
Now you can create different objects for different car, but in all, variables(property) and function (methods) in every object will same.
What will we do when we have to represent a sports car?
Because the sports car is quite different from a normal car and the properties(variable) and methods
(functions) are also more like given below:
What can we do if we want to represent the sports car?
Lets see, How We Can represent the sport Car :
2- If you are thinking that you will create another class and make all properties and functions of a Sport Car But you have to work hard again, your time and money will be double and oops also say that you can reuse your work instead of work again. Therefore it will also not work.
3- If you are thinking that you will create another class and make only those properties and functions
which is xtra in sport car.But by doing this, you will need both classes variables and function in a
single object but this is against the rule of Encapsulation because according to the Encapsulation
It's a process of combining data members and functions in single unit (object of class) called class.
therefore it will also not work
Now, we can solve the above problem by using inheritance and that's why need inheritance in classes.
what is Inheritance ?
It is a process of inheriting properties and behaviours of existing class into a new class.
Existing class=old=Parent class=Base class
New class=Child class=Derived class
Syntax of Inheritance
syntax of inheritance |
Sample Example of Inheritance
Example of inheritance |
Comments
Post a Comment