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 :



1-If you are thinking that you add all the functions in the car which is xtra in Sport Car. But by doing      this, we will not be able to access the properties and functions of the  car again. therefore it will          not  work.

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


inheritance definition
syntax of inheritance

Sample Example of Inheritance

inheritance definition
Example of inheritance



If this article is helpful to you then please like share .. thank you....




Comments

Popular posts from this blog

Notepad, Wordpad and Paint

HOW TO PASS CCC EXAM WITH ONE ATTEMPT (NIELIT)