Why inheritance is used in c




















Then both public member and protected members of the base class will become protected in derived class. Private mode : If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.

Note : The private members in the base class cannot be directly accessed in the derived class, while protected members can be directly accessed. For example, Classes B, C and D all contain the variables x, y and z in below example. It is just question of access. Single Inheritance : In single inheritance, a class is allowed to inherit from only one class. Syntax :. Multilevel Inheritance : In this type of inheritance, a derived class is created from another derived class.

Hierarchical Inheritance : In this type of inheritance, more than one sub class is inherited from a single base class. Hybrid Virtual Inheritance : Hybrid Inheritance is implemented by combining more than one type of inheritance.

For example: Combining Hierarchical inheritance and Multiple Inheritance. Below image shows the combination of hierarchical and multiple inheritance: CPP. A special case of hybrid inheritance : Multipath inheritance : A derived class with two base classes and these two base classes have one common base class is called multipath inheritance.

An ambiguity can arrise in this type of inheritance. There are 2 ways to avoid this ambiguity: Avoiding ambiguity using scope resolution operator: Using scope resolution operator we can manually specify the path from which data member a will be accessed, as shown in statement 3 and 4, in the above example.

In C, inheritance can be achieved by maintaining a reference to the base class object in the derived class object. With the help of the base class' instance, we can access the base data members and functions.

That means the derived class function will take the instance of the base class as a parameter. We are maintaining the reference to the derived class in the base class.

During function implementation, we can access the actual derived class data from the reference of the derived class. As shown in the diagram, we have declared a pointer in the base class structure holding the derived class object, and a pointer in the derived class structure holding the base class object. In the base class object, the function pointers point to the virtual functions of its own class.

This gives us the flexibility of calling the derived class function through the base class object polymorphism. For more details, check the construction on the Person and Employee objects. For the proper cleanup of objects, it uses virtual destructors. In C, this can be done by making the delete function pointer of the base class to point to the derived class destructor function.

If virtual inheritance is used, the base class is referred to as a virtual base class. Multiple base classes can be specified, separated by commas. If a single base class is specified, the inheritance model is Single inheritance.

If more than one base class is specified, the inheritance model is called Multiple inheritance. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above.

Previous Page. Next Page. Live Demo.



0コメント

  • 1000 / 1000