Inheritance of classes, Why we need to inherit the classes, , syntax to inherit the class from parent class in C++ OOP
In this tutorial, we will learn about the followings;
- Inheritance of classes.
- Why we need to inherit the classes
- Syntax to inherit the class from parent class in C++ OOP
What is inheritance of class?
We can create child classes for a class. Child classes are called derived classes and parent classes are called base classes.
Why we need to inherit the classes?
Lets an example of daily life.
Suppose John is a person with following properties;
- John have resource. e.g, home, car, bank balance and a big property.
Now John need his child for the following two reasons;
- To support in his daily work
- To get the resources before and after his death.
So, this will leads the John to think that he should have a baby.
Similarly in OOP we need to create child of the parent classes so that when we declare any variable or function in the parent class, it can be shared easily with his child class without wasting time and computer memory.
What are categories of inheritance?
There are two categories of inheritance;
- Single inheritance
- Multiple inheritance
Both of these are discussed in a separate tutorial.
Write the syntax to inherit the class from parent class?
|1||class child_class_name : Access_specifier Parent_class_name|
|3||Body of the class;|
- class is a keyword to start class. after class there is a space
- After space we put child class name, then :
- After : we put access specifier for class(private, protected, public)
- After access specifier, there is one space and then parent class name.
|Test Your Understandings|
1.class parent_class_name : Access_specifier child_class_name is the correct syntax for creating child class? YES / NO
2. There are two categories of inheritance?