Defining derived classes, Protected Inheritance, Private Inheritance, Public Inheritance Explain
Defining derived classes: -
A class that uses inheritance to gain the behavior and data of another ('base') class is called derived class. A derived class is defined by specifying its relation ship with the base class in addition to its own details. The general form of defining a derived class is as follows.
Class derived class_name : visibility-mode base-class-name
{
members of derived class;
-----------
};
1) Public Inheritance :-
Here the Child Class acquires the Protected and Public Members. And they Remain
Protected and Public in the Child Class. Its done by using “public “ keyword during Child Class definition.
• Each public member in the base class is public in the derived class.
• Each protected member in the base class is protected in the derived class.
• Each private member in the base class remains private in the derived class.
Class base-class name{ };
class child-class-name : public parent-class-name { definition of the child class};
2) Private Inheritance :-
Here the Child Class acquires the Protected and Public Members. And they
Become Private in the Child Class. Its done by using “private “ keyword during Child Class definition.
• Each public member in the base class is private in the derived class.
• Each protected member in the base class is private in the derived class.
• Each private member in the base class remains private in the derived class and hence it is visible only in the base class.
class base-class name{ };
class child-class-name : private parent-class-name { definition of the child class};
3) Protected Inheritance :-
Here the Child Class acquires the Protected and Public Members. And they Become Protected in the Child Class. Its done by using “protected “ keyword during Child Class definition.
• Each public member in the base class is protected in the derived class.
• Each protected member in the base class is protected in the derived class.
Each private member in the base class remains private in the derived class
class base-class name{ };
class child-class-name : protected parent-class-name { definition of the child class};
Labels: C++